Go to advanced search

by rpiboy
Wed Jun 26, 2019 8:19 pm
Forum: Python
Topic: List Comprehension
Replies: 1
Views: 295

List Comprehension

colors = ["red", "orange", "yellow", "greeen", "blue"]

Code: Select all

colors2 = [item.upper() for item in colors]
What is the equivalent for loop code for that list comprehension?
by rpiboy
Tue Jun 11, 2019 6:51 pm
Forum: Python
Topic: How to print without newline
Replies: 6
Views: 589

Re: How to print without newline

try raise TypeError("""The sentence should be of type str and not {}""".format(type(sentence))) and remove the unwanted white spaces, tabs and cr or lf from between the 2 text elements I know I can put it on one line and it'll print without a newline. I am wondering how I can get the code to print ...
by rpiboy
Tue Jun 11, 2019 6:05 pm
Forum: Python
Topic: How to print without newline
Replies: 6
Views: 589

How to print without newline

raise TypeError("""The sentence should be of type str and not {}""".format(type(sentence))) That code prints on two different lines. How do I get it to print on the same line? TypeError: The sentence should be of type str and not <class 'int'>
by rpiboy
Fri Jun 07, 2019 6:39 pm
Forum: Python
Topic: Remove empty string in list
Replies: 5
Views: 354

Re: Remove empty string in list

You could put a while around the input along these lines: invite_person = [] yesorno = "" while yesorno not in ['yes', 'no']: yesorno = input("Do you want to invite a person? ") yesorno = yesorno.strip() yesorno = yesorno.lower() while yesorno == "yes": enter_invite = input(str("Enter invitee's nam...
by rpiboy
Fri Jun 07, 2019 4:08 am
Forum: Python
Topic: Remove empty string in list
Replies: 5
Views: 354

Remove empty string in list

invite_person = [] yesorno = input(str("Do you want to invite a person? ")) while yesorno == "yes": enter_invite = input(str("Enter invitee's name (or just enter to finish): ")) invite_person.append(enter_invite) yesorno = input(str("Do you want to invite more people? ")) for persons in invite_pers...
by rpiboy
Wed Apr 24, 2019 3:14 am
Forum: Python
Topic: Global and Local namespace
Replies: 1
Views: 168

Global and Local namespace

def scope_test(): def do_local(): spam = "local spam" def do_nonlocal(): nonlocal spam spam = "nonlocal spam" def do_global(): global spam spam = "global spam" spam = "test spam" do_local() print("After local assignment:", spam) do_nonlocal() print("After nonlocal assignment:", spam) do_global() pr...
by rpiboy
Sat Apr 20, 2019 2:28 am
Forum: Python
Topic: Event Timer
Replies: 1
Views: 173

Event Timer

import datetime import time class TimedEvent: def __init__(self, endtime, callback): self.endtime = endtime self.callback = callback def ready(self): return self.endtime <= datetime.datetime.now() class Timer: def __init__(self): self.events = [] def call_after(self, delay, callback): end_time = da...
by rpiboy
Mon Apr 15, 2019 9:43 pm
Forum: Python
Topic: Print a specific key, value for loop dictionary
Replies: 2
Views: 203

Print a specific key, value for loop dictionary

class Options: default_options = { 'port': 21, 'host': 'localhost', 'username': None, 'password': None, 'debug': False, } def __init__(self, **kwargs): self.options = dict(Options.default_options) self.options.update(kwargs) def __getitem__(self, key): return self.options[key] options = Options.def...
by rpiboy
Sun Apr 14, 2019 8:21 pm
Forum: Beginners
Topic: Help with NFC on the raspberry pi
Replies: 2
Views: 223

Re: Help with NFC on the raspberry pi

I believe the card is MIFARE classic 1k. I google it and it says that it's only a memory storage device what does that mean?
by rpiboy
Sun Apr 14, 2019 7:34 pm
Forum: Beginners
Topic: Help with NFC on the raspberry pi
Replies: 2
Views: 223

Help with NFC on the raspberry pi

I've bought an ACR122U and followed a tutorial on how to install it on the raspberry pi. I have also bought some NFC tags. I place the NFC tags on the ACR122U and I am seeing all the memory ADDR saying unknown. How come it doesn't say read and write? I want to write a hex number to address 5, 6 or a...
by rpiboy
Tue Apr 09, 2019 1:35 am
Forum: Python
Topic: Print and return
Replies: 2
Views: 413

Print and return

def get_pages(*links): for link in links: return(link) print(get_pages("Hello.com", "World.com")) How come I only get "Hello.com" as the output? Why it doesn't output "Hello.com" "World.com"? def get_pages(*links): for link in links: print(link) get_pages("Hello.com", "World.com") When I use print ...
by rpiboy
Mon Apr 08, 2019 4:34 am
Forum: Python
Topic: Empty list as a default argument
Replies: 1
Views: 481

Empty list as a default argument

I'm trying to understand this code. //DON'T DO THIS def hello(b=[]): b.append('a') print(b) hello() ['a'] hello() ['a', 'a'] Whoops, that's not quite what we expected! The usual way to get around this is to make the default value None, and then use the iargument = argument if argument else [] idiom ...
by rpiboy
Mon Apr 01, 2019 10:32 pm
Forum: Python
Topic: For Loop
Replies: 1
Views: 297

For Loop

I have this code that outputs two different results. I want to understand what's going on. normal_list = [1,2,3,4,5,6,7,8] class CustomSequence: def __len__(self): return 5 def __getitem__(self, index): return f"x{index}" class FunkyBackwards: def __reversed__(self): return "BACKWARDS!" for seq in C...
by rpiboy
Mon Mar 18, 2019 10:35 pm
Forum: Python
Topic: sorting list
Replies: 7
Views: 479

Re: sorting list

from functools import total_ordering @total_ordering class WeirdSortee: def __init__(self, string, number, sort_num): self.string = string self.number = number self.sort_num = sort_num def __lt__(self, object): if self.sort_num: return self.number < object.number return self.string < object.string ...
by rpiboy
Mon Mar 18, 2019 9:41 pm
Forum: Python
Topic: sorting list
Replies: 7
Views: 479

Re: sorting list

Thanks all now I see how the code works. If the value is set to True it sorts the numbers and if the value is set to False it sorts by alphabet(A-Za-z)
by rpiboy
Mon Mar 18, 2019 4:32 pm
Forum: Python
Topic: sorting list
Replies: 7
Views: 479

Re: sorting list

When I put False as the value for sort_num I get [a: 4, b: 3, c: 2, d: 1] . How come it doesn't sort?
by rpiboy
Mon Mar 18, 2019 3:43 pm
Forum: Python
Topic: sorting list
Replies: 7
Views: 479

sorting list

class WeirdSortee: def __init__(self, string, number, sort_num): self.string = string self.number = number self.sort_num = sort_num def __lt__(self, object): if self.sort_num: return self.number < object.number return self.string < object.string def __repr__(self): return f"{self.string}: {self.num...
by rpiboy
Wed Mar 13, 2019 7:18 pm
Forum: Python
Topic: Index Error: Tuple index out of range
Replies: 1
Views: 387

Index Error: Tuple index out of range

Why do I get an index error when I use .format? import collections Person = collections.namedtuple('Person', 'name age gender') bob = Person(name = "Bob", age=30, gender="male") for p in [bob]: #print("%s is a %d year old %s" %p) print("{} is a {} year {}".format(p)) When I do print("{} is a {} year...
by rpiboy
Sat Mar 09, 2019 1:43 am
Forum: Python
Topic: help with zipfile
Replies: 1
Views: 190

help with zipfile

I've been reading this code for a while but totally lost on what it all does. import sys import shutil import zipfile from pathlib import Path class ZipProcessor: def __init__(self, zipname): self.zipname = zipname self.temp_directory = Path(f"unzipped-{zipname[:-4]}") def process_zip(self): self.un...
by rpiboy
Fri Mar 08, 2019 10:59 pm
Forum: Python
Topic: help() function in python
Replies: 2
Views: 327

help() function in python

Is there anything better than the help() function I can install that has better information and examples? The help() function is too technical.
I did this with help and still didn't know what it does by using the help() function.

Code: Select all

help(zip)
by rpiboy
Fri Mar 08, 2019 10:57 pm
Forum: Python
Topic: @property
Replies: 7
Views: 650

Re: @property

Why would they call themselves infinitely?
by rpiboy
Thu Mar 07, 2019 2:47 am
Forum: Python
Topic: @property
Replies: 7
Views: 650

Re: @property

Why do you put

Code: Select all

self._foo
instead of

Code: Select all

self.foo
by rpiboy
Wed Mar 06, 2019 9:14 pm
Forum: Python
Topic: @property
Replies: 7
Views: 650

@property

class Foo: @property def foo(self): return "bar" From my understanding property has a getter and a setter. a=Foo() a.foo Why does "a" return "bar"? If i have this code without @property it outputs bound method Foo2.foo of <__main__.Foo2 object at 0x7fa0d9834390 class Foo: def foo(self): return "bar"
by rpiboy
Sat Feb 16, 2019 11:27 pm
Forum: Python
Topic: Need help understanding this code
Replies: 2
Views: 240

Need help understanding this code

class EvenOnly(list): def append(self, integer): if not isinstance(integer, int): raise TypeError("Only integers can be added") if integer % 2: raise ValueError("Only even numbers can be added") super().append(integer) e = EvenOnly() e.append(2) I'm trying to understand this code and what I don't u...
by rpiboy
Tue Sep 18, 2018 2:54 am
Forum: Python
Topic: Reverse in Python
Replies: 2
Views: 454

Reverse in Python

Hi, I am trying to figure out what is going on in this code.
How come new string is now equal to "notwen"?

Code: Select all

newstring = ''
oldstring = 'Newton'
for char in oldstring:
   newstring =  char + newstring
print(newstring)

Go to advanced search