0

I have an URL template of the form

https://farm{farm-id}.staticflickr.com/{server-id}/{photo-id}_{secret}.jpg

I have retrieved the following fields and there around 1000000 of these records. After retrieving the record below

farm-id: 1
server-id: 2
photo-id: 1418878
secret: 1e92283336
size: m

I need to construct this URL

https://farm1.staticflickr.com/2/1418878_1e92283336_m.jpg

I am thinking of some approaches, should I construct a dictionary for this one?

4 Answers 4

1

You can try using str.format(*args, **kwargs):

fmt = 'https://farm{farm-id}.staticflickr.com/{server-id}/{photo-id}_{secret}.jpg'
values = { 'farm-id': '1', 'server-id': 2, 'photo-id': '1418878', 'secret': '1e92283336', 'size': 'm' }
fmt.format(**values)

Everything surrounded with { and } in format string is called "replacement field". It ca be used with kwargs for such formatting. More info here.

Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou Ivan. I started by using dictionaries. This information about kwargs is something new I learnt. Thanks!
0

A dictionary would probably be your best bet. As an alternative, check out named tuples.

Data = namedtuple('farm-id', 'server-id', 'photo-id', 'secret', 'size')
d = Data(1 ,2 ,1418878, 1e92283336, m)

1 Comment

Thanks Josh, yes I did start with a dictionary. I have to traverse through a huge file first extracting the farmids, serverids etc and then construct a url for each of them and I started by storing them into a dictionary.
0

I have managed to extract the data from the file and now I have two things, a fixed set of keys and a list of different values.

keys

['farm-id', 'server-id', 'photo-id', 'secret', 'originalformat', 'originalsecret']

values

['3', '2287', '2109698205', '20fbbcc947', 'jpg', '7a57411545']

['3', '2063', '2172469872', '8ddb85aedd', 'jpg', 'e12b952909']

['2', '1079', '830593398', '9ddca27ab0', 'jpg', 'bb91586e56']

['3', '2371', '2226919732', '248f23c5ff', 'jpg', '29f35fb0ca']

['2', '1014', '798067744', '4f043d2ea6', 'jpg', 'd739cc870b']

Is there a smarter way of merging these two lists into a dictionary after which I can use the formatting required to create the url

Comments

0

First of all, if your data is in the format you indicated you should make sure that numbers such as 1e92283336 can be interpreted are not interpreted in different way than you intended. For example with json

>>> json.loads('{"secret" : 1e92283336, "server_id" : 2, "farm_id": 1, "size" : "m"}')
{u'secret': inf, u'farm_id': 1, u'server_id': 2, u'size': u'm'}

After you successfully imported the fields to a dictionary there are multiple ways you can use string interpolation and formatting. Apart from the str.format specifier mentioned in previous answer you can use the traditional % style formatting as below (note the format specifier s after the closing parentheses)

>>> url = "https://farm%(farm-id)s.staticflickr.com/%(server-id)s/%(photo-id)s_%(secret)s.jpg"
>>> print url % {'secret': '1e92283336', 'server-id': '2', 'farm-id': '1', 'size': 'm', 'photo-id' : '1418878'}
https://farm1.staticflickr.com/2/1418878_1e92283336.jpg

You can also use Template Strings which uses the Template class from the string module. This would be more familiar to bash/perl programmers. The template string is of the form below. The placeholders (within optional braces) must be valid python identifiers so it cannot contain characters such as -. The first non-identifier character terminates the placeholder. With safe_substitute method if any placeholder value is missing it will put the original placeholder instead of raising a key error.

>>> from string import Template
>>> s = Template("https://farm${farm_id}.staticflickr.com/${server_id}/${photo_id}_${secret}.jpg")
>>> s.substitute({'secret': '1e92283336', 'server_id': '2', 'farm_id': '1', 'size': 'm', 'photo_id' : '1418878'})
'https://farm1.staticflickr.com/2/1418878_1e92283336.jpg'
>>> s.safe_substitute({'server_id': '2', 'farm_id': '1', 'size': 'm', 'photo_id' : '1418878'})
'https://farm1.staticflickr.com/2/1418878_${secret}.jpg'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.