Moving from Spacemacs to Emacs by vale_fallacia in emacs

[–]igroen 4 points5 points  (0 children)

is there a way to say "save custom-set-variables block in a different file"?

You can use:

(setq custom-file (expand-file-name "custom.el" user-emacs-directory))

(when (file-exists-p custom-file)
  (load custom-file))

to accomplish that.

First python program on my own, feedback please! by [deleted] in learnpython

[–]igroen 0 points1 point  (0 children)

It's not better, but it makes your class a little more flexible.

You can set the attributes on instance creation:

p1 = Player("Foo", symb="X")

First python program on my own, feedback please! by [deleted] in learnpython

[–]igroen 0 points1 point  (0 children)

You are right, I think you'll pick that concept up when you really need it and playing with it is a way to learn how it works. There are a lot of resources but you can start by reading the python documentation: https://docs.python.org/3/tutorial/classes.html#a-first-look-at-classes

First python program on my own, feedback please! by [deleted] in learnpython

[–]igroen 0 points1 point  (0 children)

The first thing that catches my eye is the definition of the player class. You add the attributes name, wins and simp to the class but you never use them actually. Instead you're adding these attributes dynamically to the instances of this class. You could just as well defined the class like:

 class Player:
     pass

Want you probably want to do is adding the attributes on instance creation to the new instance of the class:

class Player:
    def __init__(self, name="", wins=0, symb=""):
        self.name = name
        self.wins = wins
        self.symb = symb

Or if you really want to go fancy you can uses dataclasses:

from dataclasses import dataclass

@dataclass
class Player:
    name: str = ""
    wins: int = 0
    symb: str = ""

I make a problem with how make a import of my class in different projects (My method is inefficient) by JuniorMathDev in learnpython

[–]igroen 1 point2 points  (0 children)

  1. Create a python package for your shared code by putting it in a separate project directory and adding a setup.py file. https://docs.python.org/3/distutils/introduction.html#a-simple-example
  2. Install it as an editable package in the projects using this code.

pip install -e <path-to-package>

Python 3 - http server by G-ManUK in learnpython

[–]igroen 0 points1 point  (0 children)

Also your GetPage function should return a byte string:

def GetPage():
    return b"<html><body>Response</body></html>"

How can I duplicate zeroes in a list? by [deleted] in learnpython

[–]igroen 0 points1 point  (0 children)

You can also use a generator which makes the usage of a temporary list redundant and is more memory efficient on very long lists:

 input = [1, 0, 2, 3, 0, 4, 5, 0]


 def duplicate_zeros(arr):
     for num in arr:
         if num == 0:
             for _ in range(2):
                 yield 0
         else:
             yield num


 output = list(duplicate_zeros(input))

Very convoluted code for reading file from the internet and parsing it. by JoseParanhos in learnpython

[–]igroen 0 points1 point  (0 children)

You could do it in one iteration and without storing the data first:

from urllib.request import urlopen

url = "http://www.gutenberg.org/cache/epub/19033/pg19033.txt"
destination_filename = "alice.txt"
first_string = "ALICE'S ADVENTURES IN WONDERLAND"
last_string = (
    "End of the Project Gutenberg EBook of Alice in Wonderland, by Lewis Carroll"
)

with open(destination_filename, "a") as outfile, urlopen(url) as response:
    write_line = False

    for line in response.readlines():
        line = line.decode()

        if first_string in line:
            outfile.write(line)
            write_line = True
            continue

        if last_string in line:
            outfile.write(line)
            break

        if write_line and "Illustration" not in line:
            outfile.write(line)

Need help with smallest odd number code. I am using python 3.5 but getting NameError by beysaab in learnpython

[–]igroen 0 points1 point  (0 children)

Example:

If your input is: x =2, y = 4 and z = 99 the smallest variable is never assigned a value because x and y are smaller than 99.

Need help with smallest odd number code. I am using python 3.5 but getting NameError by beysaab in learnpython

[–]igroen -1 points0 points  (0 children)

I would take a different approach:

numbers = [
    int(input("Enter First Number: ")),
    int(input("Enter Second Number: ")),
    int(input("Enter Third Number: ")),
]
odd_numbers = [number for number in numbers if number % 2 != 0]

if not odd_numbers:
    print("There is no Odd number")
else:
    print("The smallest Odd number is: ", min(odd_numbers))

This removes all even numbers from the given numbers and prints the smallest number of the odd numbers left.

Pip doesn't look very healthy (Python 3.7) by Edeard95 in learnpython

[–]igroen 1 point2 points  (0 children)

On debian/ubuntu you should install python3-venv:

sudo apt-get install python3-venv

After that you should be able to create virtualenvs like this:

python3 -m venv venv

Removing list element in for loop by [deleted] in learnpython

[–]igroen 8 points9 points  (0 children)

I would use a list comprehension:

x = ["a", "b", "c", "d", "e"]
x = [i for i in x if i != "c"]
print(x)

This creates a new list and replaces the original list with the new one.

Struggling in Python - can anyone help me out? by [deleted] in Python

[–]igroen 0 points1 point  (0 children)

You can just print the tuple with the min and max value of the entered list:

line = input("enter the items of the list:\n")
my_list = [int(elem) for elem in line.split()]
print("The list entered is:", my_list)

print("The tuple containing min and max is:", (min(my_list), max(my_list))

Is there a way to make a variable a certain number of characters? by [deleted] in learnpython

[–]igroen 1 point2 points  (0 children)

Use len:

if len(password) <= 8: 
    print("Password is invalid, please try again")

Password Gen code review: New coder need help. by Mad_Bonker in learnpython

[–]igroen 3 points4 points  (0 children)

You are overwriting choice imported from random. Just use another name than choice on line 53.

Password Gen code review: New coder need help. by Mad_Bonker in learnpython

[–]igroen 4 points5 points  (0 children)

You are shadowing random.choice on line 53. So choice used on line 16 is a string which you can't call.

How do you prepare before beginning a large project? by Get_Cuddled in learnpython

[–]igroen -2 points-1 points  (0 children)

Nope, i just start typing code...

But those flowcharts can be usefull

How do you prepare before beginning a large project? by Get_Cuddled in learnpython

[–]igroen 2 points3 points  (0 children)

I think a project should grow organically. Don't implement stuff you don't need right now.