Dominoes posted:Start a project. Codeacademy's Python course is all you need for now. Well I'll be looking things up every ten seconds, but I suppose that's a viable way of learning the language better. I'm just concerned about developing poor programming habits out of inexperience or something like that.
|
|
# ? Jan 15, 2015 02:05 |
|
|
# ? May 9, 2024 09:43 |
|
MockingQuantum posted:Well I'll be looking things up every ten seconds, but I suppose that's a viable way of learning the language better. Not be too sarcastic, but what else did you expect to do? Magically divine the answers ahead of time? Of course you'll spend a lot of time looking stuff up when you start. Everyone does.
|
# ? Jan 15, 2015 03:05 |
FoiledAgain posted:Not be too sarcastic, but what else did you expect to do? Magically divine the answers ahead of time? Of course you'll spend a lot of time looking stuff up when you start. Everyone does. Well yes, that's inevitable, but I guess I'm asking, as someone who is completely new to programming in general: Codecademy only gets you so far, so am I better off continuing with structured self-instruction, or try a project when I have only a loose understanding of how dicts and classes (for example) work in python? I get it if there's not a good answer that someone else can give me, but I'm trying to at least minimize frustration.
|
|
# ? Jan 15, 2015 03:19 |
|
sharktamer posted:Is this gross? Yes kinda, not really because it has a return or print thing, in a debugging context that sounds normal. But it's gross because you're calling inspect.stack just to see if you're gonna sort a thing or not... That's way too ingenious.
|
# ? Jan 15, 2015 03:28 |
|
MockingQuantum posted:Well I'll be looking things up every ten seconds, but I suppose that's a viable way of learning the language better. I'm just concerned about developing poor programming habits out of inexperience or something like that. I guess you could read a lot of other people's code to get a better instinct on how to do things. Preferably cool people. You'll end up seeing a lot of features that you're not familiar with so it'll be good motivation to dig on the official documentation.
|
# ? Jan 15, 2015 03:45 |
|
MockingQuantum posted:Well yes, that's inevitable, but I guess I'm asking, as someone who is completely new to programming in general: Codecademy only gets you so far, so am I better off continuing with structured self-instruction, or try a project when I have only a loose understanding of how dicts and classes (for example) work in python? I get it if there's not a good answer that someone else can give me, but I'm trying to at least minimize frustration. You could have a look at CheckIO if you want to explore a bit further, they have a lot of structured tasks and challenges that go in lots of different directions, and generally leave you to find your own solution. Once you've written some code that solves the challenge, you can look at other people's solutions and pick up some neat tricks Having an idea of what you eventually want to do is always a good thing though, because at some point you need to jump in and get on with it. And it'll probably be bad at first, but that's all part of the learning process. You can learn to not be bad in general, pick up some useful tools and patterns, but specific projects have their own unique aspects you'll have to work out yourself, that you can't necessarily prepare yourself for. If you're stuck for project ideas though, that's a whole other problem!
|
# ? Jan 15, 2015 03:45 |
|
If you want a text, Think Python Like a Computer Scientist, check the OP or Google, is a good bit more comprehensive than I remember Codecademy being when I was going through the same process. But that and "just start something" are not exclusive. You want to learn how to build an actual application of any kind, I never really found anything better than the iterative process of "do it the wrong way, come to understand why it's wrong and in doing so learn what would have been the right way, repeat." I'm still writing garbage but it's a decidedly better class of garbage than it was when I started. The single most important general principle in my experience is to properly structure your code so it's easy to refactor as you learn (like in the Model-View-* patterns). Small specific Python things I wish I'd known earlier? Use the logging module instead of littering your code with print everywhere for quick "wait, what is that variable getting set to?" debugging. Almost every problem has a solution in itertools. Use PyCharm; it's constantly giving you "that's not how you do that" warnings about Python conventions that will stick with you even when you're writing code outside of it.
|
# ? Jan 15, 2015 04:49 |
|
MockingQuantum posted:Well yes, that's inevitable, but I guess I'm asking, as someone who is completely new to programming in general: Codecademy only gets you so far, so am I better off continuing with structured self-instruction, or try a project when I have only a loose understanding of how dicts and classes (for example) work in python? I get it if there's not a good answer that someone else can give me, but I'm trying to at least minimize frustration. Start a project. You'll get a better feel for how things work by actually using those things. Then go and see if your understanding matches the documentation and the understanding that others have developed. I think that the most effective kind of learning is studying coupled with actual work. The structured instruction gets your foot in the door, and then you do some projects (because there is no substitute for experience), and then you repeat that. In my experience, the best software developers don't just *know* everything, they review documentation and they keep an active interest in the field in addition to doing a lot of actual coding.
|
# ? Jan 15, 2015 10:09 |
|
Symbolic Butt posted:Yes kinda, not really because it has a return or print thing, in a debugging context that sounds normal. But it's gross because you're calling inspect.stack just to see if you're gonna sort a thing or not... That's way too ingenious. Yeah, I get that too. It's just I'm not sure how else I'd do this without having code repeated everywhere. edit: OK I think I've got a nicer solution. I got the solution from [url=http://stackoverflow.com/questions/2828953/silence-the-stdout-of-a-function-in-python-without-trashing-sys-stdout-and-resto]here[/here]. I'm using the contextlib solution, so now I have my "the_cool_task" function from before both printing and returning. The function that then calls it and only wants the return value then uses the with nostdout() mentioned in that post. Looks and feels a lot cleaner, even if it is still a bit gross. code:
e2: Since I can never leave anything alone, would it be possible to somehow just override/decorate fabric's execute function to not print anything? I suppose that would be doing the exact same thing as I'm already doing here anyway. sharktamer fucked around with this message at 14:30 on Jan 15, 2015 |
# ? Jan 15, 2015 11:08 |
|
As someone who is currently learning how to program, I'll be seconding most of the advice already given. I completed the course at Codecademy - and it was absolutely necessary given that I had no prior experience in programming (unless HTML/CSS counts?) - but the actual learning only truly began once I started working on a project. I reckon that most people end up reading one of the many avaliable books after completing the course, but I skipped that step and so far, so good. Getting acquainted with basic logic such as if', for's and such is necessary, but I soon realized that any practical programming would require some degree of familiarity with the existing modules/libraries, at the very least to avoid the pitfall of programming something which already exists and could be simply imported from elsewhere. With that said, just by learning through a hands-on-approach, in order to complete my project I had to familiarize myself with 7 modules, understand what MVC is, and learned how to design a GUI in the process. That's significantly more than what I would have learned in a similar timespan from doing course-work or reading a book, with the added benefit of yielding a tangible output at its conclusion, which is a great motivator. The initial "breaking in" of learning a programming language, specially the first one, is the most difficult part. Afterwards, learning-by-doing comes as a natural process of programming.
|
# ? Jan 15, 2015 17:39 |
|
I'd also suggest learning how to use git. Version control is very important whether you're working in a professional setting or just doing a throwaway project. Looking around on github is also a great way to find something interesting to work on, or you might come up with an idea that hasn't been implemented yet
|
# ? Jan 15, 2015 19:40 |
Thank you everyone for the advice, I feel like I have better perspective on how to proceed, and what pitfalls to expect. Now I'll go make Battleship or something.
|
|
# ? Jan 15, 2015 22:03 |
|
I'm in here because I'm following a Google Python tutorial, and I have a question about an exercise. The task I'm working on right now is printing a sorted list of the top 20 words in a document along with their count. Each word gets their own line. I have a dictionary because that seems the most natural structure besides a list of tuples, but since tuples are immutable, I thought a dictionary would be more efficient(not making a new tuple every time I increment the count, etc). I'm a relatively new programmer, but I know a bit of C.code:
1. Am I right that there's no built-in way to slice a dictionary? I want to cut it off when it reaches 20 items, and I could easily break the loop when an iterator reaches 20, but that seems to be coming from the C center of my brain, and I'm thinking Python might have something more elegant. I'm aware I could use something like for x in range(), but I don't know how or even if that can be combined with the loop there. I was considering actually converting and saving it to a list of tuples, since I'm thinking that's what sorted(my args) does to that dictionary anyway. If I did that I could rather easily use slice notation. 2. Since a dictionary isn't guaranteed to be sorted in any particular way, is that sorted(etc etc) expression being re-evaluated for every iteration of the loop, or does it stay in memory somewhere?
|
# ? Jan 16, 2015 00:19 |
|
Fergus Mac Roich posted:1. Am I right that there's no built-in way to slice a dictionary? I want to cut it off when it reaches 20 items, and I could easily break the loop when an iterator reaches 20, but that seems to be coming from the C center of my brain, and I'm thinking Python might have something more elegant. I'm aware I could use something like for x in range(), but I don't know how or even if that can be combined with the loop there. I was considering actually converting and saving it to a list of tuples, since I'm thinking that's what sorted(my args) does to that dictionary anyway. If I did that I could rather easily use slice notation. Python code:
quote:2. Since a dictionary isn't guaranteed to be sorted in any particular way, is that sorted(etc etc) expression being re-evaluated for every iteration of the loop, or does it stay in memory somewhere?
|
# ? Jan 16, 2015 00:30 |
|
I don't actually know what a lambda is(except vaguely) because it doesn't appear in K&R C so I guess I'll figure that out and throw it in there. The rest of your post makes perfect sense and answers my questions, thanks.
|
# ? Jan 16, 2015 00:38 |
|
Here's a nice lambda overview http://www.secnetix.de/olli/Python/lambda_functions.hawk Also it doesn't really answer your general question, but There's A Library For That! https://docs.python.org/3/library/collections.html?highlight=counter#collections.Counter
|
# ? Jan 16, 2015 00:45 |
|
Fergus Mac Roich posted:I don't actually know what a lambda is(except vaguely) because it doesn't appear in K&R C so I guess I'll figure that out and throw it in there. The rest of your post makes perfect sense and answers my questions, thanks. Python code:
code:
|
# ? Jan 16, 2015 00:49 |
|
Does anyone know if in matplotlib you can initialize subplot axes dynamically without knowing the number of subplots you are going to have before hand? I want to be able to use functions like ax.add_patches in convenience functions that just add plots in sequentially. The other option I was thinking of holding is using holding objects that would call add_patches after everything is set.
|
# ? Jan 16, 2015 01:08 |
|
Of note, you can also use key=operator.itemgetter(1) You'd use lambda or itemgetter() in this example, but you'd pass a normal function into key if you're sorting based on a more complex rule.
|
# ? Jan 16, 2015 01:14 |
|
baka kaba posted:Here's a nice lambda overview KICK BAMA KICK posted:Lambdas are anonymous functions -- one-liners you can use exactly like a function; the key of sorted and the like is the canonical example of their use in Python. Dominoes posted:Of note, you can also use key=operator.itemgetter(1) Thanks guys! The amount of crap I get "for free" in this language compared to writing it myself in C is pretty awesome. Especially not having to call a function just to allocate memory for a new struct or whatever.
|
# ? Jan 16, 2015 01:28 |
|
Yeah, it's silly huh? You might want to take a look at this too, if you have a bit of time: http://www.dabeaz.com/generators/Generators.pdf
|
# ? Jan 16, 2015 01:35 |
|
Fergus Mac Roich posted:Thanks guys! The amount of crap I get "for free" in this language compared to writing it myself in C is pretty awesome. Especially not having to call a function just to allocate memory for a new struct or whatever.
|
# ? Jan 16, 2015 01:59 |
|
KICK BAMA KICK posted:Two of the best sources of "oh, you can just do that?" are the itertools and collections modules. (Intros: itertools, collections). Speaking of getting stuff "for free"... of note for the problem posted is collections.Counter: Python code:
|
# ? Jan 16, 2015 04:54 |
|
sharktamer posted:Yeah, I get that too. It's just I'm not sure how else I'd do this without having code repeated everywhere. You could have a helper task or function that always returns the result, and have both the_cool_task and sort_output use that? It's usually a good idea to separate calculations from the input/output part for reuse.
|
# ? Jan 16, 2015 05:21 |
|
Working with SQLAlchemy, I've got something like an association object that represents a relationship between say an Employee and an Employer:code:
Thing is, I'd like to attach some data to each position, ideally to access like JobTitles.Manager.base_salary. I'm kinda stuck on the least gross way to do it -- do I have to just define a simple enum (whether a Python enum or the native SQLAlchemy type) in one place, and then a dictionary mapping the value the enum assigns to each job title to a dict or NamedTuple or other object containing the attributes associated with each job title? Like: code:
|
# ? Jan 16, 2015 20:31 |
|
(I don't have a lot of database experience) It sounds like maybe you'd want a JobTitle table? I'm not sure if the fact that they're unlikely to change is reason against doing so. Hell it may even be a good reason to enshrine them in the database itself.
|
# ? Jan 16, 2015 21:08 |
|
SurgicalOntologist posted:(I don't have a lot of database experience) It sounds like maybe you'd want a JobTitle table? I'm not sure if the fact that they're unlikely to change is reason against doing so. Hell it may even be a good reason to enshrine them in the database itself. This was going to be my suggestion. If you want data associated with each type of JobTitle (things like BaseSalary and MinYearsExperience and Description or whatever else), then you may as well create a table for that, and then have a JobTitleID in your employee table.
|
# ? Jan 17, 2015 01:30 |
|
Thanks for the input. I'm not sure about putting the job title data in its own table though; while I don't need to edit those attributes at runtime I would like to be able to edit them during the development process and touching the db(s) every time seemed undesirable. This probably makes more sense if I mention that this isn't like a business application, it's a prototype for a dumb sim/game idea. Thinking about it more I'm leaning back toward just defining a class for each job title with its data as class attributes, each with a unique discriminator value. Then construct a dict mapping the discriminator to the class, put a column for the discriminator on the employment record table and give the employment record objects a job_title property that gets the job title class object from the dict by its value from the discriminator column. That's less weird than it sounded to me at first.
|
# ? Jan 17, 2015 02:23 |
|
Question about generators. I have a generator that yields lists of words(one list per call, obviously). Is there any way to use another generator expression to yield each element of each list? I feel like the solution is staring me in the face but there's some kind of gap in my understanding of generators that prevents me from seeing it. I want to avoid writing a function if possible. edit: Wow, I think the answer might actually be in the itertools module you guys helpfully pointed out. double edit: Okay, I thought the answer was itertools.chain(), but I couldn't get that to work. Here's an insanely stupid construct that did work: code:
I'm really liking generators. Check out my new function: code:
code:
Fergus Mac Roich fucked around with this message at 02:59 on Jan 17, 2015 |
# ? Jan 17, 2015 02:25 |
|
Fergus Mac Roich posted:Question about generators. I have a generator that yields lists of words(one list per call, obviously). Is there any way to use another generator expression to yield each element of each list? Python code:
Yes, Python chained for loops in a single expression are a bit yodaspeak. I'm not sure why. ShadowHawk fucked around with this message at 03:28 on Jan 17, 2015 |
# ? Jan 17, 2015 03:26 |
|
Fergus Mac Roich posted:edit: Wow, I think the answer might actually be in the itertools module you guys helpfully pointed out. The structure is basically like nesting for loops, so this: Python code:
Python code:
e- beaten, but also chain should work, but there are two versions of it itertools.chain(*iterables) Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted. Used for treating consecutive sequences as a single sequence. classmethod chain.from_iterable(iterable) Alternate constructor for chain(). Gets chained inputs from a single iterable argument that is evaluated lazily. (This is an amateur explanation coming up) This is a little tricky, but you see that asterisk in the first one? That unpacks a collection of arguments into in the individual elements. It turns 'hey chain, handle this box of stuff' into 'hey chain, handle this and this and also this'. The difference is that chain will treat the box (say your collection of iterables) as the -single- iterable you want it to operate on, and it will happily hand you each element inside it - but those elements are the things you actually wanted it to iterate over. So you need to unpack that collection as you hand it to chain, so it gets multiple elements instead of the container. If that makes sense! Like this: Python code:
baka kaba fucked around with this message at 04:21 on Jan 17, 2015 |
# ? Jan 17, 2015 04:13 |
|
ShadowHawk posted:What you want is equivalent to: I kind of figured that I might be able to do it with a generator function, but in this case I decided(somewhat arbitrarily) that I preferred to be as succinct as I could - I had a strong sense that a generator expression could do it. I'll have to read the style guide to get a better sense of how variable names are supposed to look, I was just using whatever matched the concept I had in my head of what was happening with the function. baka kaba posted:... Good post, thanks. I should have read the docs more closely, I made a totally wrong assumption about the asterisk and then proceeded to miss the alternate version of chain. I'll swap my solution for chain, actually, because I think it's much more clear.
|
# ? Jan 17, 2015 04:27 |
|
Fergus Mac Roich posted:double edit: Okay, I thought the answer was itertools.chain(), but I couldn't get that to work. Here's an insanely stupid construct that did work:
|
# ? Jan 17, 2015 04:31 |
|
Also y'know Counter() takes an iterable too - you can just stick your word generator in there, like Counter(words) and it will feed it and build up the totals itself. No need to increment things! You don't even need to create a variable, you can just make your generators and then do return Counter(words) . See the lines of code vanish before your eyes like dissipating steam! Well, back to Java for me
|
# ? Jan 17, 2015 05:17 |
|
KICK BAMA KICK posted:Lambdas are anonymous functions -- one-liners you can use exactly like a function; the key of sorted and the like is the canonical example of their use in Python. quote:
|
# ? Jan 17, 2015 05:32 |
|
The line between brevity and incoherence is a difficult one to grasp for me, I think.
|
# ? Jan 17, 2015 05:35 |
|
Ah, but sometimes brevity is quite coherent. Not only can Counter take an iterable, but str.split splits on blank lines and spaces. Therefore, your entire code can be replaced with this: Python code:
E: you were probably referring to lambdas, so disregard my "incoherence" jabs. And because I like functional programming in Python and think the toolz library is really cool: Python code:
Python code:
SurgicalOntologist fucked around with this message at 06:02 on Jan 17, 2015 |
# ? Jan 17, 2015 05:47 |
|
Very true. I'll think of some more apps to write and be back when I have more questions. Thank you all.
|
# ? Jan 17, 2015 05:49 |
|
ShadowHawk posted:The latter isn't more pythonic, it's explicitly prohibited in the Pep-8 style guide. If you need to use the function more than the line it's created on you should just use a def statement.
|
# ? Jan 17, 2015 06:08 |
|
|
# ? May 9, 2024 09:43 |
|
SurgicalOntologist posted:Ah, but sometimes brevity is quite coherent. Don't write Lisp in Python, please.
|
# ? Jan 17, 2015 06:51 |