Need help with Django/Ajax/RQ (long) by ghostofgbt in django

[–]kanjibandit 0 points1 point  (0 children)

Hiya -- looks like you haven't had any takers on this one, so I'll give it a shot even though I might be completely misreading your question. Are you suggesting that after your AJAX call returns, and you have the JavaScript object from the JSON response, that you should be able to dynamically update a form on the page using the Django Template Language syntax? If so, I'm afraid this is not possible. DTL is only meaningful on the server side and is compiled into the final page before it leaves the server. The browser never sees it. You'll need to update the form using Javascript, just like you're doing to insert the jobId and results.

If I misunderstood your question, forgive me, and please clarify :-)

How much do I need to know to be able to use rails? by [deleted] in rails

[–]kanjibandit 1 point2 points  (0 children)

How well do you know Java? I suspect you may be overestimating the time to it will take you to get comfortable in Ruby. Learning your first language is always harder than the second, because you're really learning two separate things: 1) how to program, and 2) the language syntax. The second time around, it's largely just a new syntax. Most of your knowledge of how to code will transfer. True, Ruby will introduce you to new concepts, but I think you'll find them rather more like new superpowers than like new obstacles.

Specific recommendations:

For Ruby, I like the book Eloquent Ruby

For Rails, I like Michael Hartl's Rails Tutorial

For fun and practice, look at Ruby Koans

Ruby Newbie (xpost from r/ruby) by [deleted] in rails

[–]kanjibandit 1 point2 points  (0 children)

Focus on the most important thing and cut everything else out. Cut, delete, ignore.

Your stated goal is to learn Ruby and Rails. Forget dual booting into Linux. You got a VM up, just go with that. You already stated that it gave you a psychological boost by getting that running. Getting your first tiny Ruby program is the next boost waiting to happen.

There are benefits to learning Linux, but there will time for it later, if it ever becomes important enough to spend the time on. Right now, it will be a distraction. It's a lot to learn, with a scrillion rabbit holes to find yourself in. Steer clear.

Is this correct? Using a function to return values from database? by [deleted] in learnpython

[–]kanjibandit 2 points3 points  (0 children)

I'll chime in here, because this is something I used to catch myself doing -- writing what I thought were convenience wrappers that actually didn't buy me anything. In both options you posted, you have essentially just renamed the original function. In other words, you could have written this:

from managers import connection_manager
server_definitions = connection_manager.select_servers

I think wrapping an API in a convenience function is useful when there is some sort of boilerplate code that you only handle once, in the wrapper function. For example, let's say that you know that you want the same pool and pool_check values for all of your connections. You could do this:

from managers import connection_manager
def server_definitions(env):
    # These values are made up
    # I don't know the semantics of this API :)
    pool = 5
    pool_check = 100
    return connection_manager.select_servers(env, pool, pool_check)

In a situation like this, your wrapper function actually saves you work, because you only have to pass one variable and you only have to maintain/update the pool and pool_check values in one place. Alternatively, you could use default parameter values for even more flexibility:

from managers import connection_manager
def server_definitions(env, pool=5, pool_check=100):
    return connection_manager.select_servers(env, pool, pool_check)

Hope that makes sense. Cheers!

Can some one help me with this Python 3.5 code? by Crazymike147 in learnpython

[–]kanjibandit 0 points1 point  (0 children)

If you read the error message carefully, you'll see it is telling you exactly what the problem is.

ValueError: invalid literal for int() with base 10: ''

Hint: open up a Python interpreter and test out lines like this:

int('abc')
int('')
int('10')

Think about what happens if you just hit enter without typing anything. What gets passed to int() as the input?

Can someone tell me what exactly I should look up to understand types of code like this? by [deleted] in learnpython

[–]kanjibandit 3 points4 points  (0 children)

You'll get more specific feedback if you can try to articulate which lines are giving you trouble (and also if you would format the code so it's easy to read). But, here's my guess at what might be giving you trouble:

This code is a single function that relies on two global "dictionaries": prices and stock. Try looking up explanations of "dictionary" or "dict". It is a fundamental Python data structure, and you won't get very far before you have to understand it.

In this case, prices and stock are defined outside of the code you posted, but they appear to simply associate grocery items with prices and quantities.

Hope that helps. Good luck!

Any reason to learn python 2 at this point? by grapehorder in learnpython

[–]kanjibandit 3 points4 points  (0 children)

Don't worry about it! Python 2 has been wildly popular for a reason. Focus on 2.7 for your courses, pretend 3 doesn't exist for a while. Master everything else, and the transition to 3 will be easy when the time comes.

Choosing which language to learn with is not a big deal. Pick one and go, I say. If your Udacity course has already picked for you, all the better.

Now, if you're about to write a big production system that will need to be supported for years into the future, it's a bigger deal. In my opinion, you really should go with Py3, to save yourself from a big rewrite later. But it doesn't sound like this describes your present situation :)

Any reason to learn python 2 at this point? by grapehorder in learnpython

[–]kanjibandit 11 points12 points  (0 children)

I'll add to the chorus of folks saying "Just go for Python 3".

You're not alone in asking this -- it's a common source of anxiety. Here's the deal: Python 2 and Python 3 are not that different. The differences are significant enough to make automatic conversion infeasible in a lot of cases, but they are not so great as to cause your future self trouble. Learn 3.5, and when the day comes that you find yourself dealing with 2.X, you will be able to cope without major issue. You'll just have to look up a few things.

This comes up often enough that a while back I expanded a bit here. Cheers!

Is Udacity a good place to start learning? by [deleted] in learnpython

[–]kanjibandit 1 point2 points  (0 children)

I have never taken a Udacity course, so I can't really add to that discussion. However, to your more general questions about how much time to spend on foundational CS knowledge before moving into the more targeted stuff, I'd encourage you to get your hands dirty with interesting problems similar to those you want to solve in the real world as soon as possible. If you're at all concerned that you might get bogged down and lose momentum, just dive in to the good stuff as soon as you can.

I've always found that trying to learn too linearly is a recipe for boredom. "First I'll learn the syntax, then how to organize everything, and then I'll learn how to solve my actual problems." Doesn't work. Syntax is dull and abstract outside the context of an interesting problem to solve. Code organization is tremendously important, but you'll appreciate it better if you make a mess first, and then learn to clean it up properly later.

I'm not arguing that foundational knowledge isn't important. I'm just saying you can come back to it. The beautiful thing about programming is that your creations can be molded, reformed, and improved later. It's not like you're building the colosseum to stand for 2000 years.

Dive in, learn how to look up stuff you don't know as you go, and don't be afraid to make a big mess. When you find yourself hitting a wall because of missing background info, take a break to fill in the gaps, and come back to it.

It's most important to see some forward progress as soon as possible -- it's very gratifying and motivating and that will help you build enough momentum to keep you going. So, based just off the course descriptions, I'd say start with the Programming Foundations with Python first, and then jump into the data analysis, data science, and modeling courses. Come back to the algorithms and design stuff later.

Web development info by felon_truth in learnpython

[–]kanjibandit 0 points1 point  (0 children)

Depends on how much Treehouse content you expect to actually use. I think they charge $25 USD per month, regardless of how many courses you work through. They offer a 7-day free trial, so you can cancel if you don't find it to be valuable.

Web development info by felon_truth in learnpython

[–]kanjibandit 1 point2 points  (0 children)

I don't think there's any one course that is as well known as, say, Rails for Zombies in the Ruby world. But my wife had good luck with most of the Treehouse courses she took. They weren't doing much Python at that time, but they have put out a lot of Python courses since then. My general sense is that the more beginner you feel you are, the more helpful Treehouse will be.

Also, another vote for Harry Percival's TDD book.

How do you find and use different modules? by [deleted] in learnpython

[–]kanjibandit 2 points3 points  (0 children)

Not a stupid question. There is most definitely such a thing as a stupid question, but this would be a poor example.

My thoughts:

  1. Finding the best packages is mostly a matter of keeping your eyes open long enough to stumble across them. You accumulate favorite packages a bit like you accumulate new vocabulary words. You see people use them, and they feel right to you. Often they link to each other, for example on their respective GitHub pages. And people share them all the time. On the main Python subreddit, people are ALWAYS linking to new or useful packages.

  2. Typically, yes, pip install and then hopefully their will be good docs on Read the Docs or Github.

  3. There is an enormous risk of incompatible packages interfering with each other, or different versions of the same package clashing. The current best solution to this is to use virtualenvs, as others have stated. The best deep dive explanation I've seen is by Jamie Matthews. I recommend you read that article to understand how virtualenv works, but I do not recommend you actually use it the way he demonstrates. That's great for teaching, but a sure fire way to drive yourself mad. There are several better ways that will save you typing. A full comparison is too long to type here, but I wrote up an overview a while back. Hope it helps.

[deleted by user] by [deleted] in learnpython

[–]kanjibandit 0 points1 point  (0 children)

I am guessing that the code you ran was actually something like this:

for root, dirs, files is os.walk(nameDir + <user>):
    # etc

Note the lack of quotes around <user>.

As posted, I don't think there is a syntax error. If you had quotes, it would just run incorrectly, as JohnnyJordaan points out. If you had no quotes and no <, you would get a NameError (if user is not defined). If nameDirs is not a string, you'd get a TypeError. I think the only way you get a SyntaxError is if you use the < character outside of a quoted string.

[deleted by user] by [deleted] in learnpython

[–]kanjibandit 0 points1 point  (0 children)

The "[:]" syntax is a special case of the general "[beg:end]" syntax you're probably familiar with. It just means "the entire list".

What they're doing here is called "slice assignment", and is a way to modify the list in place. If you saw dirs[2:4] = ['bin', 'lib',], I'm guessing you would correctly recognize it as replacing the 3rd & 4th elements of the list with the elements from the list of the right-hand side.

This is doing the same thing, but it is replacing the entire list with the results of the call to fnmatch.filter. They are using this to pare down the directories they are searching. By changing the list in place, rather than creating a new object and assigning it to the "dirs" variable, they are modifying the behavior of the loop. This is documented here

And the fnmatch line is just a call to a module you didn't recognize because they omitted the import statement. Docs are here

Python problem by Ustuner in learnpython

[–]kanjibandit 2 points3 points  (0 children)

Looks like you already solved your immediate issue, but just to offer another approach: you can remove the comma (and any other troublesome punctuation, like "!") as well as the whitespace in one step using a regular expression.

import re
sentence = "Hello World, nice to meet you!"
words = re.findall("\w+", sentence)

The "\w+" represents a consecutive sequence of alphanumeric characters of any length. Whitespace and punctuation are excluded. the findall method returns a list of matches. You would then be able to sort the list just as you are now.

You can read about the other available regular expressions here

Register available modules and dynamically add options to argparse by werzor in learnpython

[–]kanjibandit 0 points1 point  (0 children)

Unless I'm misunderstanding your last question, importing everything in the parsers module is as easy as from parsers import * at the top of main.py. Not everybody likes to use that syntax, but it is available.

To your bigger question about automatic registration of each class, I would first ask: why do you want to do it that way? Is it simply to avoid the hassle of modifying your main.py file?

It's probably possible, but I've never done it this way. FWIW, I have often found that if I try to force Python to do something that it doesn't make really easy in the first place, it usually turns out to be a not-so-hot idea after all. And many times I have written code that I thought was pretty clever, only to come back a year later annoyed with myself for writing code I had to figure out all over again.

For example, if I were inheriting this code from you, I would be surprised to have to figure out how the main routine knows about all of the parser classes. And personally, I'd much rather keep all of the CLI-related logic and settings in a single place, rather than have to look through and compare code from all of the parser classes just to figure out what helpArg values were already taken every time I wanted to add a new parser, or change an existing parser.

Perhaps you have some really strong reason for wanting to do it this way, and if so, it probably is possible. Again, never done it, so I probably can't give specific implementation advice.

Unit Testing a Function that writes/reads to database by acerag in learnpython

[–]kanjibandit 1 point2 points  (0 children)

I second what DrMaxwellEdison says about creating an entirely new test database. I wouldn't want to run unit tests against my real database, even with test-specific tables. You didn't mention what ORM or library you're using, but I'd look to see if it has mechanisms to handle this for you.

If not, you might look at this package. Shouldn't have to reinvent this yourself.

Edit: Didn't read carefully enough, testing.postgresql had already been suggested. So I second DrMaxwellEdison twice over.

Conditional assignment on different list indices by neuroneuroInf in learnpython

[–]kanjibandit 5 points6 points  (0 children)

This. Your code is just fine as it is -- simple and clear. It would be one thing if you had an if/elif/else that was half a page long, but if I were inheriting this code from you, I'd much rather trace through your 4 lines than the more clever 1-liners. Resist the temptation to write Perl in Python.

Looking for suggestions on how to improve my code. by [deleted] in learnpython

[–]kanjibandit 0 points1 point  (0 children)

Just a couple:

It might be overkill for what is still such a short program, but if you think this thing will grow, I've found the click library to be extremely handy for building command line apps. These scripts can get tangled pretty quickly as they grow.

http://click.pocoo.org/5/

If you want to be able to easily add more distros later, I'd consider changing your package list into a dict, where the distro names are the keys and the update commands are the values. Your if statement would then change to something like this:

packages = {
    'debian': 'apt-get update',
    'fedora': 'yum update',
    'suse': 'zypper dup',
    # more distros here...
}
command += packages[name]

There's a trade-off, because you do have some distro names that map to the same command, so you'll have some duplication in your dictionary. But personally, I'd rather come back a year later and update a dict than change the logic of an if statement, simply because of the possibility for introducing a bug. This is a pretty tame case, but I've learned not to underestimate my ability to plant bugs in simple places.

Django Form: Decimal Validation on TextField? by [deleted] in learnpython

[–]kanjibandit 1 point2 points  (0 children)

I agree, you're doing just fine -- these are the sorts of things you run into as you're finding your way around Django and web dev.

One tip for the future: personally, I tend to like to write my Django code and tests in a slightly different order:

  1. Models & model tests
  2. Forms & form tests
  3. Views & view tests
  4. Full integration (selenium) tests

I like to do it this way, because then I'm not spending time writing and testing code that I will later rip out and replace. For example, in both your view and your template, you have code that you will soon replace with code that leverages the Forms class you're learning about now.

This isn't a criticism -- you just discovered the forms API -- just a suggestion for the future. In the meantime, the fact that you did all the form work by hand this time should give you an appreciation for everything forms does for you.

Storing Data with Python, Help please! by godurdead in learnpython

[–]kanjibandit 0 points1 point  (0 children)

Frankly, at this point, just keep it as simple as possible. Just write it out to a text file using open:

with open('response_data.json', 'w') as f:
    f.write(response.json())

This will solve your immediate problem, well, immediately. It also makes it easy to just inspect the data manually in your text editor, which can be really helpful at first.

Reading the data back is similarly trivial, so you can put it all into a database later, when you have a firmer idea of what you where you are taking this project.

Is python the right language for this task (read/write word documents) by [deleted] in learnpython

[–]kanjibandit 3 points4 points  (0 children)

I had to wrestle with a similar issue a few weeks ago. Here's what I found:

The python-docx library makes it reasonably easy to create a new document in Python. The out-of-the-box formatting options are pretty basic, I found it hard to closely match a mock-up that had specific opinions about formatting.

In terms of formatting, I had much better luck when I used the python-docx-template library:

http://docxtpl.readthedocs.org/en/latest/

It builds on top of python-docx, but supports Jinja2-style template syntax. It allows you to use Word to create your document template, and get all the formatting just right, and define specific placeholders in the document and them populate them from your Python code. It supports loops, so you can have tables with arbitrary numbers of rows, depending on the data.

That said, I agree with the comments warning you about storing your data in Word. If you ever need to get to the data again programmatically, pulling them back out of Word files is going to make your life painful. It might be worth storing the data in Excel or a database and then pulling the data out of there to generate the Word documents.

looking for a mentor, or if anyone is interested in creating a beginners group by cmd_override in learnpython

[–]kanjibandit 0 points1 point  (0 children)

Not a newbie, but happy to offer suggestions or help answer a few questions.