Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
It catches all exceptions. Including KeyboardInterrupt.

Adbot
ADBOT LOVES YOU

FoiledAgain
May 6, 2007

JOHN SKELETON posted:

Care to elaborate? Why is hasattr bad?

In 2.x it can return False when you might not expect it to. Some explanation here.

TURTLE SLUT
Dec 12, 2005

Huh. That seems surprisingly dumb. Weird how quick Googling didn't give me anything on it either.

salisbury shake
Dec 27, 2011
I have a bunch of pure python data structures that will be wrapped in QObjects so they can play nicely with Qt models/views.
Some of the objects only have attributes that are static strings. With these objects, how can I wrap their attributes dynamically without manually enumerating and decorating a bunch of getter methods?

Python code:
class QWrapper(QtCore.QObject):
	def __init__(self, python_obj):
		super().__init__()
		self.data = python_obj
		for attribute in self.data.__dict__.keys():
			getter = lambda attribute=attribute: getattr(self.data, attribute)
			value = pyqtProperty(str, getter, constant=True)
			setattr(self, attribute, value)
Works in my head but not irl

Thermopyle
Jul 1, 2003

...the stupid are cocksure while the intelligent are full of doubt. —Bertrand Russell

Suspicious Dish posted:

I would never write or use this code ever,.

Yeah, I wouldn't either. Sleepy posting is my version of drunk posting.

I laughed when I read what I wrote last night. I don't know what I was thinking.

In sane land, I'd probably just try/except, or if I was feeling lazy use basestring.

edit: Upon further thought I think my idea was fine. The code is just kinda crap. Sometimes you need to make sure your objects have the behavior you want when you get them from third party libraries or APIs. You want to fail early instead of in 2 weeks at 2 AM when your server takes a rarely-used branch. I'd special-case a string check with isinstance(obj, basestring) and then try/except some sequence operations on the object to see if its a sequence unless you feel confident that you can assume if it's not a string then it's a list or tuple or whatever. I'm kind of ambivalent about checking for attributes like __getitem__ or __getslice__ or whatever, so I wouldn't yell at someone for doing that either.

However, with regards to the original question about how to handle it when you might have a string or a list/tuple... Like I said in my sleepy post, its hard to say without knowing more about the API design, but in what my gut tells me is the most common case, I'd have a function with the basic functionality which is called by a function for a string and a function for a list/tuple where the string function makes a list/tuple out of the string.

If the only difference between the two code paths is that you might get a list of strings or just one string, I might do away with two separate functions, check if it's a string or a list right off the bat and if so, put it in a list and then continue on with the rest of the function code assuming it's dealing with a list.

Thermopyle fucked around with this message at 19:27 on Jan 8, 2014

SirPablo
May 1, 2004

Pillbug
Anyone have experience working with shapefiles and numpy? I'd like to make a mask based on a shapefile, then analyze data using that mask. I'm really at a loss where to look.

For example, if I have an array (gridded analysis) of current temperature across the CONUS, I'd like to take a shapefile of Texas and determine what the average temperature is in it based on the temperature array. I've tried to follow this example but just isn't clicking. Any suggestions?

duck monster
Dec 15, 2004

"Hi after spending nearly $30K on a 3 month long intranet development project written in Django, we cant afford to spend $70 a month on a decent server system and IT has deemed you have to use this lovely shonkyhost PHP3 cpanel account. What do you mean its not compatible? Our IT guy says it even supports squirrelmail so its plenty advanced, sorry its been decided".

"Well it was in the loving spec document, and without it this can't be deployed, but you'll still be required to pay your loving bill. You cunts."

I loving hate this industry with the fire of 1000 suns.

edit: "And no you can't upload the ipad client that will be used by only 30 people all in your staff to the app store! Yes you can't refund the dev account, no I won't change my mind its not up to me"

duck monster fucked around with this message at 08:12 on Jan 9, 2014

John DiFool
Aug 28, 2013

Yes, hi mr. duck monster what is your question sir? You must please with asking the question.

duck monster
Dec 15, 2004

John DiFool posted:

Yes, hi mr. duck monster what is your question sir? You must please with asking the question.

I'm just venting dude. Feel the hateflames with me. Clients are hell.

SurgicalOntologist
Jun 17, 2004

How strict are JetBrains with their open source licenses for PyCharm? I would love the remote interpreter feature. I have an open source project I'm actively developing, but their requirements include:

JetBrains posted:

  • Your OS project's community is active. This means that you have recent activity in your newsgroups or forums.
  • Your OS project has a web site. The web site has a News section that is being kept up to date, or links to a social network account used to announce updates of the project.

I'll hit the 3-month mark soon, so I'll apply then and find out what my chances are, but these two are serious hurdles. Maybe the second one if adding a "News" header to my readme on BitBucket counts, but the only other users so far are my colleagues. Maybe I can get someone to sign up for BitBucket and post an issue or something, and that will count as a "forum" post.

SurgicalOntologist fucked around with this message at 17:32 on Jan 9, 2014

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.

John DiFool posted:

Yes, hi mr. duck monster what is your question sir? You must please with asking the question.
Please with asking the question so I may the needful

Vulture Culture
Jul 14, 2003

I was never enjoying it. I only eat it for the nutrients.

SurgicalOntologist posted:

How strict are JetBrains with their open source licenses for PyCharm? I would love the remote interpreter feature. I have an open source project I'm actively developing, but their requirements include:


I'll hit the 3-month mark soon, so I'll apply then and find out what my chances are, but these two are serious hurdles. Maybe the second one if adding a "News" header to my readme on BitBucket counts, but the only other users so far are my colleagues. Maybe I can get someone to sign up for BitBucket and post an issue or something, and that will count as a "forum" post.
Are you tracking bugs on BitBucket already? I'm pretty sure that counts.

SurgicalOntologist
Jun 17, 2004

Misogynist posted:

Are you tracking bugs on BitBucket already? I'm pretty sure that counts.

Nope, but that's doable. I'll find some bugs and post them as issues.

salisbury shake
Dec 27, 2011

SurgicalOntologist posted:

How strict are JetBrains with their open source licenses for PyCharm? I would love the remote interpreter feature. I have an open source project I'm actively developing, but their requirements include:

Extremely lenient. Have a repository with recent commits and a license file in the root directory. I filled out their little form and got my license within a day

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

John DiFool posted:

Yes, hi mr. duck monster what is your question sir? You must please with asking the question.

Someday we will be able to harness Duck Monster's impotent rage into a source of energy for the entire planet.

duck monster
Dec 15, 2004

Lumpy posted:

Someday we will be able to harness Duck Monster's impotent rage into a source of energy for the entire planet.

Stay in this god forsaken industry long enough and you too will lie at wake imagining various ways to murder rich people using their neckties.

Pollyanna
Mar 5, 2005

Milk's on them.


Luckily, everyone in my generation already dreams of doing that very thing. :allears:

Dominoes
Sep 20, 2007

Does anyone know how to make the python XML module not fill close tags with extraneous data?

I'm recreating an XML file type that draws things on a map. Here's the output:

XML code:
<FVDrawingOverlay>
	<event version="2.0" type="t-x-f" uid="{1}" time=... etc>
		(stuff)
	</event version="2.0" type="t-x-f" uid="{1}" time="... etc>
</FVDrawingOverlay>
I'm pretty sure the close tag should just say </event> without repeating the info after it. The files generated by the program that I'm creating end without repeating, and Firefox flags my file as bad xml and won't display it without deleting the extra text.

Code I used to generate the XML, with the bulk of the body removed:
Python code:
    import xml.etree.ElementTree as ET

    drawing = ET.Element('FVDrawingOverlay')
    event = ET.SubElement(drawing, '''event version="2.0" type="t-x-f"... etc''')
    point = ET.SubElement(event, 'point lat="{0}" lon="{1}" ce="1" hae="9999999" le="0"'.format('52', '0'))
    detail = ET.SubElement(event, 'detail')
    fvdrawingellipse = ET.SubElement(detail, ... etc)
   
    xml.write('test.xml')

Opinion Haver
Apr 9, 2007

ElementTree thinks you're generating an XML element whose name is "event version...". You should be doing something like

Python code:
event = ET.SubElement(drawing, "event")
event.attrib = {"version": "2.0", "type", "t-x-f", ...}
instead.

Dominoes
Sep 20, 2007

Thanks, that works. Apparently you can also append the attribute dictionary as an optional additional argument to SubElement and Element.

Dominoes fucked around with this message at 12:05 on Jan 12, 2014

unixbeard
Dec 29, 2004

I need to read a bunch of excel files in python, just reading no writing/creation. It seems like there are a few packages, xlrd and openpyxl, before I dive in does anyone have opinions or advice for/against either of them?

Dominoes
Sep 20, 2007

I'm looking for advice on how to store database information for a Django web app on RHCloud. I'm uploading raw text info of temporary flying restrictions, and want to store and reference them. Columns would be something like this: "id, start date, end date, category, min alt, max alt" etc. There would probably be a few thousand entries.

The basic program's done; it parses the text from a webpage, sorts it out with regex, generates the required files etc. I want to store the data in a database to make sure that older info that isn't expired is still used, even if it's no longer present in the uploaded text.

Should I try Django's models system? It uses databases, but the tutorials I've read are mostly about using it for an admin page, storing user logins etc. This is a webapp so it can run on work computers (RHCloud is miraculously not blocked), and interface with GMaps' JS API. I'm not sure the models system would be appropriate.

Should I do the database manually? I've used sqlite before. I've read that you shouldn't use it for production use, but this would only be used by 1 person at a time, so it would probably work.

Dominoes fucked around with this message at 12:57 on Jan 12, 2014

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Dominoes posted:

I'm looking for advice on how to store database information for a Django web app on RHCloud. I'm uploading raw text info of temporary flying restrictions, and want to store and reference them. Columns would be something like this: "id, start date, end date, category, min alt, max alt" etc. There would probably be a few thousand entries.

The basic program's done; it parses the text from a webpage, sorts it out with regex, generates the required files etc. I want to store the data in a database to make sure that older info that isn't expired is still used, even if it's no longer present in the uploaded text.

Should I try Django's models system? It uses databases, but the tutorials I've read are mostly about using it for an admin page, storing user logins etc. This is a webapp so it can run on work computers (RHCloud is miraculously not blocked), and interface with GMaps' JS API. I'm not sure the models system would be appropriate.

Should I do the database manually? I've used sqlite before. I've read that you shouldn't use it for production use, but this would only be used by 1 person at a time, so it would probably work.

You can write your own ModelManager to handle hooking up the ORM to a remote DB. We are in the process of writing a Django app that uses a custom ModelManager coupled with SQLAlchemy to use a (*shudder*) Azure Cloud MSSQL store for our data.

Pie Colony
Dec 8, 2006
I AM SUCH A FUCKUP THAT I CAN'T EVEN POST IN AN E/N THREAD I STARTED
Do you guys think this would be a good interview question?

code:
>>> x = []
>>> x.append(x)
What is x now? What is the length of x?

evensevenone
May 12, 2001
Glass is a solid.
No.

I don't even know why that's allowed.

Pie Colony
Dec 8, 2006
I AM SUCH A FUCKUP THAT I CAN'T EVEN POST IN AN E/N THREAD I STARTED
why would it be disallowed? anyway, my intention was a short simple question that could demonstrate some knowledge of how python handles references and values

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

evensevenone posted:

No.

I don't even know why that's allowed.

Disallowing cyclic data structures in a language with mutation would be dumb and really slow.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Pie Colony posted:

Do you guys think this would be a good interview question?

code:
>>> x = []
>>> x.append(x)
What is x now? What is the length of x?

It's a very bad question. I had to think about this one for a bit, because the obvious answer (length=1) seemed so obvious that it was probably wrong.

Also, arcane Python trivia is probably not going to get you the best-of-breed programmers who can build the things you want them to build.

evensevenone
May 12, 2001
Glass is a solid.

Plorkyeran posted:

Disallowing cyclic data structures in a language with mutation would be dumb and really slow.

Yeah, now that I think about, you're right.


It still probably isn't a very good question.

suffix
Jul 27, 2013

Wheeee!
I wouldn't call it arcane, but yeah, if you're going to ask about Python gotchas, ask about the ones most people actually encounter, like mutable default arguments.

BeefofAges
Jun 5, 2004

Cry 'Havoc!', and let slip the cows of war.

I think it would be a decent interview question if instead of expecting them to know the answer and get it right, you just listen to their ideas on what might be the possible behaviors and why the different options would make sense or not. The answer wouldn't matter, just the thought process involved.

Pie Colony
Dec 8, 2006
I AM SUCH A FUCKUP THAT I CAN'T EVEN POST IN AN E/N THREAD I STARTED

Suspicious Dish posted:

It's a very bad question. I had to think about this one for a bit, because the obvious answer (length=1) seemed so obvious that it was probably wrong.

Also, arcane Python trivia is probably not going to get you the best-of-breed programmers who can build the things you want them to build.

i won't ask it but knowing that x is a reference to an empty list instead of an empty list is hardly arcane

Dren
Jan 5, 2001

Pillbug
I think it's such a weird edge case that it is trivia and therefore not worth asking in interviews.

If you want to gauge someone's understanding that python is pass-by-reference ask them a more straightforward question. If you're asking it just to see their thought process in answering it then I guess it's ok.

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe

Pie Colony posted:

i won't ask it but knowing that x is a reference to an empty list instead of an empty list is hardly arcane

It's a bad question to prove that point, though.

Pollyanna
Mar 5, 2005

Milk's on them.


Now the question is, why do we get this

code:
>>> y = [1]
>>> y.append(y)
>>> y[1]
[1, [...]]
instead of this

code:
>>> y = [1]
>>> y.append(y)
>>> y[1]
[1]
if y[1] is supposed to be a list?

Suspicious Dish
Sep 24, 2011

2020 is the year of linux on the desktop, bro
Fun Shoe
The list contains itself.

Pollyanna
Mar 5, 2005

Milk's on them.


So it's a recursive list...? What is that used for?

evensevenone
May 12, 2001
Glass is a solid.
Basically nothing, which is why it's a bit of a dumb question.

Sometimes you do have hierarchical data structures where children have references to their parents, which means you have to be a little careful traversing them or copying then, but it's not that bad and this question doesn't really lead you to think about that anyway.

Gmaz
Apr 3, 2011

New DLC for Aoe2 is out: Dynasties of India
What do the three dots inside the bracket mean?

Adbot
ADBOT LOVES YOU

evensevenone
May 12, 2001
Glass is a solid.
It's the printer in the interpreter being smart and telling you that it spotted a circular reference.

  • Locked thread