I once had to write a sequence design engine for a complicated molecular biology problem, and it involved bundling permutations of potentially hundreds of sequences (each with a unique ID) and then running computationally intensive calculations on them. I used frozensets for rapid membership checking of "have I already looked at this combination and if so, what was the result?" Using a (frozen)set removed the need for any kind of sorting, which was nice, considering the number of possible combinations could be enough to set your computer on fire.
Oh, do I understand you correctly, that you stored the sequences you already checked as frozen sets and then hashed them, to get results for sequences you have already calculated, independent of the order of the actual sequence?
I use the all the time. I use sets a lot in my programs as a lot of problems can be approached in terms of sets. Another thing frozensets are useful for is being able to put them into a set. You can't have a set of sets, but you can have a set of frozensets.
"it states, or it shows, or there's frozenset written" Not really to the point! Also the type annotations in this video make the code much less readable and the whole concept is much more difficult to understand than it should be. Finally gets to the point around 3:45
It also can be used to simulate mathematical definition of ordered pairs, which is defined as set of sets. Being hashable, it can be in set or frozenset so can simulate mathematical concepts
I'm mostly a hobbyist programmer so whenever I feel the need for a "hashable set" I just call `tuple(sorted(the_set))` (works well at least for smallish data).
whenever I want a constant that is a collection, I almost always use a frozenset. The reasons: - immutable: secure and small memory size (like a tuple) - `in` operation is much faster than a tuple when the collection size becomes large enough (O(1) like a set)
Also, I think you can make sets of sets using frozenset. You cant with just set. There many math scenarios that involves set of sets, of sets... So it can be useful. Also, immutables are good practice. Check out some functional programming principles
❤I hope you can explain Python libraries such as ms4, bs4, re, mechanicalalsoup, socket, and more libraries such as platform, and more and more, please.🌹❤😢
I first encountered frozensets when I was researching whether it was possible to do something similar to the permissions example. My main problem with it is superficial - it's quite a lot of characters every single time you want to create one, which can make for some awkward-looking code, particularly if you're trying to follow line length standards. While watching this is occurred to me that you could make a class with convenience __getitem__ and __setitem__ methods to handle converting keys to frozensets if necessary. Probably not the best idea in a performance-sensitive context, but might be handy elsewhere.
Maybe something like this (though I'm sure it can be massively improved): ``` class SetKeyedDict(dict): @staticmethod def sanitize_key(key): if isinstance(key, str): key = frozenset((key,)) elif not isinstance(key, frozenset): key = frozenset(key) return key
def __init__(self, *args, **kwargs): # TODO: Sanitize initial input super().__init__(*args, **kwargs) def __getitem__(self, key): return super().__getitem__(self.sanitize_key(key)) def __setitem__(self, key, value): super().__setitem__(self.sanitize_key(key), value) def __delitem__(self, key): super().__delitem__(self.sanitize_key(key)) def __contains__(self, key): return super().__contains__(self.sanitize_key(key)) ``` I leave type hinting as an exercise for the reader 😉 (I gave it a go, but quickly realized it would get quite confusing given its generic nature. I'm sure the recent Carberra video on Generics would have helped, but I didn't want to get completely sidetracked.) It would be really handy if there were a way to preprocess all key references without having to overload every method, but from some brief searching I couldn't find a way.
can you make a tutorial on using async function in a non async function. like calling a async function in a non async function using loops, like loops=asyncio.get_running_loop() i seriously don't know how to say that, 😅 I don't wanna use asyncio.run() on every function in a class
I once had to write a sequence design engine for a complicated molecular biology problem, and it involved bundling permutations of potentially hundreds of sequences (each with a unique ID) and then running computationally intensive calculations on them. I used frozensets for rapid membership checking of "have I already looked at this combination and if so, what was the result?"
Using a (frozen)set removed the need for any kind of sorting, which was nice, considering the number of possible combinations could be enough to set your computer on fire.
Oh, do I understand you correctly, that you stored the sequences you already checked as frozen sets and then hashed them, to get results for sequences you have already calculated, independent of the order of the actual sequence?
Hashing is amazing, instead of checking every single element, you can just check the hash and rest assured the equality is correct almost always
I use the all the time. I use sets a lot in my programs as a lot of problems can be approached in terms of sets.
Another thing frozensets are useful for is being able to put them into a set. You can't have a set of sets, but you can have a set of frozensets.
Probably will never use it but I will sure brag about knowing about frozensets
+
I love how you explain the concepts. Straight to the point 😊
"it states, or it shows, or there's frozenset written"
Not really to the point!
Also the type annotations in this video make the code much less readable and the whole concept is much more difficult to understand than it should be.
Finally gets to the point around 3:45
It also can be used to simulate mathematical definition of ordered pairs, which is defined as set of sets. Being hashable, it can be in set or frozenset so can simulate mathematical concepts
I'm mostly a hobbyist programmer so whenever I feel the need for a "hashable set" I just call `tuple(sorted(the_set))` (works well at least for smallish data).
whenever I want a constant that is a collection, I almost always use a frozenset. The reasons:
- immutable: secure and small memory size (like a tuple)
- `in` operation is much faster than a tuple when the collection size becomes large enough (O(1) like a set)
Finally someone mentioning frozenset and it's uses. Unfortunately, the projects where it would have been useful I didn't know about it at the time.
Also, I think you can make sets of sets using frozenset. You cant with just set.
There many math scenarios that involves set of sets, of sets...
So it can be useful.
Also, immutables are good practice. Check out some functional programming principles
❤I hope you can explain Python libraries such as ms4, bs4, re, mechanicalalsoup, socket, and more libraries such as platform, and more and more, please.🌹❤😢
can you please talk about the memoryview type, its so confusing
up till now, i'm using it for a key of a dictionary when doing a value mapping, when the initial value is a set
I first encountered frozensets when I was researching whether it was possible to do something similar to the permissions example. My main problem with it is superficial - it's quite a lot of characters every single time you want to create one, which can make for some awkward-looking code, particularly if you're trying to follow line length standards.
While watching this is occurred to me that you could make a class with convenience __getitem__ and __setitem__ methods to handle converting keys to frozensets if necessary. Probably not the best idea in a performance-sensitive context, but might be handy elsewhere.
Maybe something like this (though I'm sure it can be massively improved):
```
class SetKeyedDict(dict):
@staticmethod
def sanitize_key(key):
if isinstance(key, str):
key = frozenset((key,))
elif not isinstance(key, frozenset):
key = frozenset(key)
return key
def __init__(self, *args, **kwargs):
# TODO: Sanitize initial input
super().__init__(*args, **kwargs)
def __getitem__(self, key):
return super().__getitem__(self.sanitize_key(key))
def __setitem__(self, key, value):
super().__setitem__(self.sanitize_key(key), value)
def __delitem__(self, key):
super().__delitem__(self.sanitize_key(key))
def __contains__(self, key):
return super().__contains__(self.sanitize_key(key))
```
I leave type hinting as an exercise for the reader 😉
(I gave it a go, but quickly realized it would get quite confusing given its generic nature. I'm sure the recent Carberra video on Generics would have helped, but I didn't want to get completely sidetracked.)
It would be really handy if there were a way to preprocess all key references without having to overload every method, but from some brief searching I couldn't find a way.
can you make a tutorial on using async function in a non async function.
like
calling a async function in a non async function using loops, like loops=asyncio.get_running_loop()
i seriously don't know how to say that, 😅
I don't wanna use asyncio.run() on every function in a class
Thank you 😊
Could one use Enums to make dictionary keys?
Can you make a video on cycle
what i am grateful to you
frozenset is hashable, can be key of dict
set is not
fstring with =
variable declaration using :