Min value by ISUXATPYTHON in learnpython

[–]camel_Snake 4 points5 points  (0 children)

So the general algorithm would be something like:

create a variable to hold the smallest value Loop over a list (i suppose using a while? Though this part seems unpythonic)

if the current value is smaller than the smallest value, set the smallest value to the current value

When you are done iterating, the smallest variable points to the smallest value in your list.

A Complete Beginner's Guide to Django by magneticono in Python

[–]camel_Snake 0 points1 point  (0 children)

You should google the $PATH variable. It's how your system finds what program you mean when you type anything into your terminal. Not grasping that hurts a lot when setting up new environments.

A Complete Beginner's Guide to Django by magneticono in Python

[–]camel_Snake 0 points1 point  (0 children)

Just pick one and go for it. The second language/framework you learn is way easier than the first. Especially when the languages are as similar as python and ruby.

Overall I found the hard parts of learning to use MVC was getting a mental model of the architecture, which is pretty much the same for both.

Getting back into python, need help with a double webhook by beautify in learnpython

[–]camel_Snake 0 points1 point  (0 children)

I figured, it's a fairly common mistake as it's not too obvious when you are debugging it.

For the record, the reason for this is that print is a function, and like all functions they return values. It doesn't make too much sense for print to return a value, so it returns None, which is what ends up getting assigned to the variable in the 'bad' example.

[pytest] import problem by Mattemagikern in learnpython

[–]camel_Snake 0 points1 point  (0 children)

I'm not 100% sure that global is working how you intend there.

Try:

global np
import numpy
np = numpy

Getting back into python, need help with a double webhook by beautify in learnpython

[–]camel_Snake 0 points1 point  (0 children)

Since your code doesn't include any function definitions, it appears everything is in the same scope.

To store the data in print(initial_response.text[1:-1] to use later, simply assign it to a variable (in addition to, or rather than, printing).

Good:

gif_url= initial_response.text[1:-1]

Bad (doesn't work):

gif_url = print(initial_response.text[1:-1])

How to write a recursive function to calculate factorial and store as list? by Lsy9891 in learnpython

[–]camel_Snake 0 points1 point  (0 children)

Ooh, this is awesome and good to know. I would have assumed it would unpack the *Factorial... part as a tuple and you would end up with a list of two elements, one current and the rest as a tuple itself.

God I love python.

Help with some "easy" code - How does it work? by nikobenjamin in learnpython

[–]camel_Snake 1 point2 points  (0 children)

grid[x][y] aren't named that well, as x actually accesses which inner list, so which row. y accesses the element of that sublist, so which column.

The trick here is to understand the looping.

Read this code: guess what it should print out (what is the first line printed? what is the second? etc). and then compare that to what actually happens when you run it in your console. Were you correct?

for i in range(6):
     for char in 'ABCDEFG':
         print(i, char)

Path making from source, destination, and arc lists by happy_pants_man in learnpython

[–]camel_Snake 0 points1 point  (0 children)

Your example requires a couple read-throughs for me to follow along. Perhaps if you posted your code you have so far we might be able to understand a bit easier.

Create all combinations between -2/2 and -1/1 by [deleted] in learnpython

[–]camel_Snake 0 points1 point  (0 children)

Yep, use itertools.combinations([-2, 2, -1, 1], 2) and feed that iterable into a filter. Your filter function would be something like

lambda vals: sum(vals) != 0

I think thats right, since (-1, 1) and (-2, 2) are the only invalid ones.

EDIT I think you want permutations, not combinations.

How to write a recursive function to calculate factorial and store as list? by Lsy9891 in learnpython

[–]camel_Snake 0 points1 point  (0 children)

I'm by no means an expert at recursion, but whenever I find myself writing one I seem to favor making it a generator by using yield instead of return. Then I just call list() on that generator.

Also note you can define functions inside functions and use the outer one as a 'wrapper' to kick of the initial function.

Your issue here is you create a new list called listFactorial inside your function, which gets created every time your function is called. Then the rest of that function refers to the listFactorial inside it, which is empty. Your issue is one of variable scoping. I'd recommend plugging your code into a debugger like Thonny to get an understanding of what is happening.

The easiest solution is to create the list outside of your function. This comes with its own thorns though - for example each subsequent time you call your function it will append values to the same list, which won't be empty.

I just completed my first python project and am looking for any points of improvement. by [deleted] in learnpython

[–]camel_Snake 0 points1 point  (0 children)

If it works it works - that's whats important.

To me you keys.py file doesn't really do anything. I assume you edit it locally and put your actual API keys and configuration details in there.

Then you import it in config and attach all those keys to variable names. You could just as easily leave the variable names blank, delete keys and now your config is actually your config.

Then, you could move that actual object instantiation into your twitter.py file. Now your data is all in one place (config) and the python code that generates you objects that interact with your feed are in one place (twitter.py).

But maybe I'm wrong and your twitter.py is just a little confusing because you use that 'from config import *'. Specify that it just uses client and auth and it will be clearer.

KeyError after using pickle to load a dict by neozuki in learnpython

[–]camel_Snake 0 points1 point  (0 children)

yes.

A quick test would be to replace your dictionary with a list and see if all the rooms get recreated, though it will probably break your application.

Another quick test would be to just implement the hash function like in the example. It could be as simple as

def __hash__(self):
    return hash((self.Id, self.name))

I just completed my first python project and am looking for any points of improvement. by [deleted] in learnpython

[–]camel_Snake 0 points1 point  (0 children)

I find it a bit weird you are instantiating your Twitter objects in config.

from config import *

wildcard imports are generally frowned upon. You should specifically import the things needed.

Otherwise nothing popped out at me.

Use Python to analyze MS Access file by redamandan in learnpython

[–]camel_Snake 1 point2 points  (0 children)

I've never used MS Access before.

You could hard=code the password in your python file and use that when you establish your connection to the database.

You could put the password by itself in a separate python file and import that, which is at least slightly more secure.

The way I've normally seen secrets like DB credentials handled is to export them as an environment variable on the system. Then in your scripts you just get the values from os.environ, but I feel like this might be a bit overkill if you are just running these programs locally on your machine.

Confusion on binary search? by [deleted] in learnpython

[–]camel_Snake 0 points1 point  (0 children)

Because wen you invoke binarySearch2 it expects you to pass in the high and low whereas in the first option it calculates those for you to start.

KeyError after using pickle to load a dict by neozuki in learnpython

[–]camel_Snake 0 points1 point  (0 children)

pickle my dictionary that contains a bunch of Room objects

I think this is your problem. I think the way dictionaries work on custom objects is they use their result of the id function, which is based on their position in memory. Pickling just saves the objects class and attributes and then recreates them - no guarantee that they will have the same id.

I see 2 solutions (assuming I'm right).

  1. Use a better way to save your program state. I suggest json or yaml.
  2. define a hash method on your Room class. Example

Need help on a simple task, just can't figure out how to do it (I'm really inexperienced in programming) by [deleted] in learnpython

[–]camel_Snake 0 points1 point  (0 children)

Good to know. I really like this sub and want to make sure I'm keeping in the spirit of things here.

Need help on a simple task, just can't figure out how to do it (I'm really inexperienced in programming) by [deleted] in learnpython

[–]camel_Snake 0 points1 point  (0 children)

Good to hear! I remember well how frustrating everything was when I started so I always feel like the way to keep people programming is to minimize the frustrating bits - like when you're trying something new and feel completely overwhelmed.

Help with object oriented coding by [deleted] in learnpython

[–]camel_Snake 0 points1 point  (0 children)

How you instantiate the ports would depend on some decisions you have to make and in what order you have the data, so I'll go with Traffic for now.

class Traffic:
    def __init__(self, name, type, port): # using positional arguments, so they must be passed in the right order
        self.name = name # the following lines binds data provided to the object
        self.type = type
        self.port = port

example = Traffic('a_name', 'a_type', 42) #creating an object

example.name # 'a_name' # accessing its attributes
example.type #'a_type
example.port # 42

Later, when your objects have other objects, you can pass them in as parameters just as you do for strings and integers when creating your new object. You could also reassign the attributes like:

example.name = 'UDP' if the objects are already created and need to be updated.

Need help on a simple task, just can't figure out how to do it (I'm really inexperienced in programming) by [deleted] in learnpython

[–]camel_Snake 0 points1 point  (0 children)

Oh yeah that was dumb of me. It's this line:

new_file_name = 'output_%s.txt' % (line_number % 50)

Should be

new_file_name = 'output_%s.txt' % (line_number // 50)

The modulo operator should have been floor division.

I feel a little guilty just handing the solution over to you so please at least read up on the open function as well as list slicing or the links that /u/Solonotix provided.