How to get substring keys on firebase? - python-3.x

I'm working with firebase on pyhon3. I wanted to know if there's a way I can check if a substring is present in the firebase keys. For e.g. if I have a string 'he', and I have keys in my firebase for 'hell' and 'hello'. I can perform a get for 'he', but how can I get the others as well. And i dont want to get the entire parent and search on python as it is a fairly large database.
How can I go about to get them? I know this is not how firebase is designed. But remodeling will take a lot of time. So can it be done, and if so how?

Try using in like this:
>>> x = 'hello'
>>> y = 'll'
>>> y in x
True
So if you have a value you can assign, and you know what you want to check against, this is the easiest way to go about this in Python.

Related

Python beginner has some problems with the spotipy search function

i am trying to set up a basic tool which is supposed to do following:
1. Search for Musicans in Spotify
2. Print results in a way i can use them for stuff (e.g. show output in a window or something like that)
Unfortunatly i cant wrap my head around following Problem:
import spotipy
import sys
from spotipy.oauth2 import SpotifyClientCredentials
client_credentials_manager = SpotifyClientCredentials(client_id='',
client_secret='')
spotify = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
result = spotify.search(q='Korn', limit=5)
for i, t in enumerate(result['tracks']['items']): # way i dont understand
print (' ', i, t['name'])
print(result) #way i would like to do it
The code can print a list in this enumerate fashion but isnt capable of printing it normally. The thing is I dont understand the way enumerate does this.
Can someone please give me some insights how the search function and enumerate work and maybe show me an elegant solution so i can print this list?
I don't know about the spotipy module so I'll have to assume a few things, based on what you said works or doesn't work.
So... your variable named 'result' seems to behave like a dictionary. You can index it with keys ('tracks', 'items'), (as shown there: result['tracks']['items']), and this seems to give you back stuff that behaves like a list containing stuff that behaves like a dictionary, that among other things contains the key 'name' (as shown there: t['name']).
To illustrate how enumerate works, here's an example:
>>> hello = ['h', 'e', 'l', 'l', 'o']
>>> print(list(enumerate(hello)))
[(0, 'h'), (1, 'e'), (2, 'l'), (3, 'l'), (4, 'o')]
It essentially gives you back tuples of each index in the list, and what is at that index.
Also note that Python has a principle called 'duck-typing'. It says that “If it looks like a duck and quacks like a duck, it must be a duck.”. That's why I don't worry to much about what type an object is, what I care about is what it behaves like.
Now, when you say something like:
for index, element in enumerate(elements):
You're using what's called unpacking. It lets you separate the tuples that enumerate gives you into two variables.
Then if we wanted to simply do print(result), the class that let you instantiate result would need to implement either the __repr__ or __str__ methods. In it, you would have all the logic that's contained in your for loop, grabbing the data at the appropriate keys in the dictionary-like object and returning a correctly formatted string. But it looks like this class comes from an external library, so I'm not sure it would be a good idea to implement these yourself (I don't have experience doing that yet).
Hope that my explanations were at least a little bit helpful.

How do I avoid "'zip' object is not reversible" errors when migrating to Python 3?

In my recently migrated from 2 to 3 Python code I have
list(reversed(zip(*positions)))
which generates the error
TypeError: 'zip' object is not reversible
I can fix this by changing the problematic code to
list(reversed(list(zip(*positions))))
but this seems like the wrong way to go about it.
What is the correct way to revers a zip in Python 3?
reversed is used to iterate on the list. It doesn't create a list on purpose, because it's often used just to iterate backwards on elements, not to create lists.
That's why you have to use list on it to create a list. And it needs a sequence to be able to get to the last element directly so you have to do list(zip()) in python 3.
Maybe you could shorten
list(reversed(list(zip(*positions))))
to
list(zip(*positions))[::-1]
it creates a list directly without the need for reverse so it's probably slightly faster too.

Python 3 - string to list (I know it has been asked but I can't get anything to work)

I have an assignment, similar to scrabble. I have to check if a subset is in the set. can only use a letter once. so if subset has 2t and the set has 1t it is false.
My problem is, I used 2 inputs to allow people to enter the subset and set, but that create a string no breaks between the letters which mean split or list won't create a LIST with individual letters. (at least I can't find any way.)
My plan was something like
wordset = word.lower().split()
subset = letters.lower()
for i in range(len(subset)):
if i in subset and in set:
set.remove(i)
I know that properly won't work but until I can get it into a list or someone gives me a hint how to do it with string I can't start testing it. Sorry for so much writing.
If you wish to get a list of characters in a given string you can use a list comprehension:
characters = [x for x in some_string]

Add Dictionary Keys and Values to Redis List

I am trying to add the current dictionary to a Redis list using a dictionary comprehension and then to print out the first (aka current) keys and values of that list. I say current because this is a process I will be continuing with a while loop to have the list building over time, but I have to always access the first keys/values.
I am sure I am totally butchering this, but this is what I have:
adict = {"a":1,"b":2,"c":3}
{rserver.rpush("list",value) for value in adict}
print(float(rserver.lindex("list",0)))
I need to get a list of both keys and values back.
Help would be MUCH appreciated. Thanks!
I am not quite positive on what your redis-list should contain (please include your expected result in the question), but assuming it should at the end of inserts look something like this ["a:1", "b:1", "c:1"], you can achieve this with
adict = {"a":1,"b":2,"c":3}
for key,value in adict.items():
rserver.rpush("list", ":".join([key, value]))
print(float(rserver.lindex("list",0))) #>>> "a:1"
(as you have not included what interface rserver exactly is, it is a bit hard to guess on its exact behavior)

Create list of tkinter canvas lines/points

Many lines and points possible that I would like to be able to track when I line/point has been moused over. Is there any short codeable way of doing it or do I half to come up with hundreds/thousands of different element names.
I've tried
self.z[0].canvas.create_line()
self.z[1].canvas.create_line()
as well as
self.z(0).canvas.create_line()
self.z(1).canvas.create_line()
to only get back an error saying something like z can't be an integer, aka you can't do that stupid:)
Is there anyway to set up a nice for loop and create the lines/points and then be able to test test them once they are created. I can test the points the way I want to be able to test them but I would just like an easier way of creating the lines/points.
Worst case scenario is there a way of setting up something like
self.z1.canvas
self.z2.canvas
self.z3.canvas
but have 1,2,3 each be able to be increased through a for loop? I'm not sure if I have ever seen something like what I'm suggesting be made mention of or not.
Every time you create an item on a canvas, it returns a unique id. You can store that id in a list.
self.lines = []
for x in range(1000):
item = self.canvas.create_line(...)
self.lines.append(item)
That being said, you don't need to keep any of these in an array to " track when I line/point has been moused over.". You can set up bindings for that.

Resources