cooperhewitt / label-book

Generates a markdown file for use with InDesign
1 stars 0 forks source link

A better for loop #4

Closed micahwalter closed 9 years ago

micahwalter commented 9 years ago

This https://github.com/cooperhewitt/label-book/blob/master/label-book.py#L50-L59 was kind of a quick hack yesterday to make it work.

A better way to do this ( read cleaner ) might be something like this

for index in range(len(data)):
    obj_id = data[index]['id']

and so on... this way you dont need the i=0 and i=i+1 stuff..

copea commented 9 years ago

Lists in Python are iterators. You can just do:

for obj in data: print obj["id"]

crouching keyboard / hidden typos

On Jul 7, 2015, at 09:38, Micah Walter notifications@github.com<mailto:notifications@github.com> wrote:

This https://github.com/cooperhewitt/label-book/blob/master/label-book.py#L50-L59 was kind of a quick hack yesterday to make it work.

A better way to do this ( read cleaner ) might be something like this

for index in range(len(data)): obj_id = data[index]['id']

and so on... this way you dont need the i=0 and i=i+1 stuff..

— Reply to this email directly or view it on GitHubhttps://github.com/cooperhewitt/label-book/issues/4.

micahwalter commented 9 years ago

Yes but to be able to add more data to the list you need an index right?

M

Sent from my iPhone

On Jul 7, 2015, at 1:17 PM, copea notifications@github.com<mailto:notifications@github.com> wrote:

Lists in Python are iterators. You can just do:

for obj in data: print obj["id"]

crouching keyboard / hidden typos

On Jul 7, 2015, at 09:38, Micah Walter notifications@github.com<mailto:notifications@github.commailto:notifications@github.com> wrote:

This https://github.com/cooperhewitt/label-book/blob/master/label-book.py#L50-L59 was kind of a quick hack yesterday to make it work.

A better way to do this ( read cleaner ) might be something like this

for index in range(len(data)): obj_id = data[index]['id']

and so on... this way you dont need the i=0 and i=i+1 stuff..

— Reply to this email directly or view it on GitHubhttps://github.com/cooperhewitt/label-book/issues/4.

— Reply to this email directly or view it on GitHubhttps://github.com/cooperhewitt/label-book/issues/4#issuecomment-119273277.

copea commented 9 years ago

Not sure what you're trying to do but:

.append("foo") crouching keyboard / hidden typos On Jul 7, 2015, at 10:33, Micah Walter > wrote: Yes but to be able to add more data to the list you need an index right? M Sent from my iPhone On Jul 7, 2015, at 1:17 PM, copea mailto:notifications@github.com> wrote: Lists in Python are iterators. You can just do: for obj in data: print obj["id"] crouching keyboard / hidden typos On Jul 7, 2015, at 09:38, Micah Walter mailto:notifications@github.commailto:notifications@github.com> wrote: This https://github.com/cooperhewitt/label-book/blob/master/label-book.py#L50-L59 was kind of a quick hack yesterday to make it work. A better way to do this ( read cleaner ) might be something like this for index in range(len(data)): obj_id = data[index]['id'] and so on... this way you dont need the i=0 and i=i+1 stuff.. — Reply to this email directly or view it on GitHubhttps://github.com/cooperhewitt/label-book/issues/4. — Reply to this email directly or view it on GitHubhttps://github.com/cooperhewitt/label-book/issues/4#issuecomment-119273277. — Reply to this email directly or view it on GitHubhttps://github.com/cooperhewitt/label-book/issues/4#issuecomment-119277207.
micahwalter commented 9 years ago

Hehe, ok, so this is me confusing PHP and Python - basically you can just do something like the following:

args = {'exhibition_id': '68744913', 'has_images': '1', 'page':'1', 'per_page':'5'}
rsp = api.execute_method('cooperhewitt.exhibitions.getObjects', args)

data = rsp['objects']

for item in data:
    item['micah'] = "was here"

print pprint.pformat(data)

It seems that Python passes everything by reference so altering "item" in this case also alters "data"

I guess this means "Python is fun!"

copea commented 9 years ago

It means it's really easy to shoots yourself in the foot. See also: from copy import deepcopy

crouching keyboard / hidden typos

On Jul 7, 2015, at 15:54, Micah Walter notifications@github.com<mailto:notifications@github.com> wrote:

Hehe, ok, so this is me confusing PHP and Python - basically you can just do something like the following:

args = {'exhibition_id': '68744913', 'has_images': '1', 'page':'1', 'per_page':'5'} rsp = api.execute_method('cooperhewitt.exhibitions.getObjects', args)

data = rsp['objects']

for item in data: item['micah'] = "was here"

print pprint.pformat(data)

It seems that Python passes everything by reference so altering "item" in this case also alters "data"

I guess this means "Python is fun!"

— Reply to this email directly or view it on GitHubhttps://github.com/cooperhewitt/label-book/issues/4#issuecomment-119366787.

ghost commented 9 years ago

merged