Carberra
Carberra
  • 417
  • 2 358 111
The CORRECT way to work with temporary files in Python
Working with temporary files can be a must in certain situations, but are you working with them correctly? If not, you're opening yourself up to security vulnerabilities. In this video, we'll be using the `tempfile` module to create temporary files easily and securely!
00:00 - Intro
01:01 - Creating a named temporary file [1]
04:13 - Creating a temporary directory [2]
04:57 - Creating a temp file within a temp directory
06:05 - Other interfaces in the tempfile module
07:25 - Using this with a Pytest fixture [3-4]
[1] docs.python.org/3/library/tempfile.html#tempfile.NamedTemporaryFile
[2] docs.python.org/3/library/tempfile.html#tempfile.TemporaryDirectory
[3] alembic.sqlalchemy.org/en/latest
[4] docs.pytest.org/en/stable
-
If you want to see more of me, join my Discord server!
discord.gg/5vVgQpK
I get a lot of people asking, so here's my setup!
• Visual Studio Code: ua-cam.com/video/PBQspYkkksU/v-deo.html
• Terminal: ua-cam.com/video/4RuGK3w6Mbs/v-deo.html
• Desk/recording gear: kit.co/Carberra
• PC build: uk.pcpartpicker.com/list/TmzGYN
Affiliations (I may earn a commission):
• Codecrafters -- 40% off any plan: app.codecrafters.io/join?via=parafoxia
• Keeper Password Manager -- 30% off your first year: keeper.io/r/EX4FB1C
-
If you have any questions, don't hesitate to ask in the comments! I'll try and answer as soon as I can, providing someone else hasn't already done so.
#python #coding #howto
Переглядів: 2 864

Відео

Capturing a function's LOCAL variables?!
Переглядів 3,5 тис.21 годину тому
Trust me, this does actually have a use case! You would ordinarily not be able to access the local variables of a function unless you returned them, but this function captures the internal state of the decorated function at return time, allowing you to verify it's what it should be! Very neat, but definitely a hack! 00:00 - Intro [1] 01:06 - Demonstration [2] 03:13 - Implementing the capture fu...
Are descriptors the most MISUNDERSTOOD part of Python?
Переглядів 10 тис.14 днів тому
Descriptors, in many conversations I've had with people, are typically considered one of the most difficult parts of Python. I would posit however that they're moreso misunderstood rather than difficult once you get your head around them, there's not much to remember! They're also an incredibly powerful and important concept, as huge swathes of the Python standard library is written using them,...
5 SIMPLE Pytest tricks to improve your tests
Переглядів 3,1 тис.21 день тому
In production code, the tests are often as important as the code, if not more. Without proper testing, how will you know if your code will ever work in the wild? In this video, I share five top tips to improve your tests and to make sure they're working properly! 00:00 - Intro 00:41 - Context [1] 01:33 - Parametrization [2] 03:22 - Finding edge cases w/ Hypothesis [3] 05:41 - Tracking coverage ...
Numba makes your code FASTER with ONE decorator
Переглядів 4,1 тис.28 днів тому
Try Keeper Password Manager free for 30 days, then get 30% off when you upgrade using the code CARBERRA30: www.keepersecurity.com/personal.html - There has been plenty of discussion around Python's new JIT since it dropped, but did you know there are other ways of getting a JIT into Python? One of the simpler ways is by using Numba, which allows you to JIT functions by using a single decorator!...
I baked the Hello World cake (but everything went wrong)
Переглядів 487Місяць тому
I baked the Hello World cake (but everything went wrong)
Liked Pydantic? You'll LOVE Msgspec
Переглядів 12 тис.Місяць тому
Liked Pydantic? You'll LOVE Msgspec
Pydantic is OP, here's why
Переглядів 22 тис.Місяць тому
Pydantic is OP, here's why
Will Python SKIP these versions?
Переглядів 3,7 тис.Місяць тому
Will Python SKIP these versions?
5 tips for CLEAN Python code
Переглядів 3,1 тис.2 місяці тому
5 tips for CLEAN Python code
Python's contextlib is a HIDDEN GEM
Переглядів 11 тис.2 місяці тому
Python's contextlib is a HIDDEN GEM
Partial functions in Python are SUPER NEAT
Переглядів 4,5 тис.2 місяці тому
Partial functions in Python are SUPER NEAT
SIMPLIFY your code with decorators (+ typing)
Переглядів 3,8 тис.2 місяці тому
SIMPLIFY your code with decorators ( typing)
Implementing OAuth 2.0 from SCRATCH
Переглядів 1,9 тис.3 місяці тому
Implementing OAuth 2.0 from SCRATCH
Python has BIOMETRIC support?!
Переглядів 2,2 тис.3 місяці тому
Python has BIOMETRIC support?!
What's new in Python 3.13?
Переглядів 80 тис.3 місяці тому
What's new in Python 3.13?
I tried a FREE Copilot alternative
Переглядів 2,3 тис.3 місяці тому
I tried a FREE Copilot alternative
Python is Awesome - Series 2
Переглядів 1,6 тис.3 місяці тому
Python is Awesome - Series 2
HYBRID callables in Python?! - Jankfest
Переглядів 1,7 тис.3 місяці тому
HYBRID callables in Python?! - Jankfest
Python is Awesome - Series 1
Переглядів 3 тис.3 місяці тому
Python is Awesome - Series 1
Using Redis/Redict/Valkey in Python (2024 tutorial)
Переглядів 2 тис.3 місяці тому
Using Redis/Redict/Valkey in Python (2024 tutorial)
My Visual Studio Code setup! (2024)
Переглядів 2,4 тис.4 місяці тому
My Visual Studio Code setup! (2024)
Wait, you CAN use braces with Python?!
Переглядів 3,3 тис.4 місяці тому
Wait, you CAN use braces with Python?!
The history of Visual Studio Code's WORST theme
Переглядів 47 тис.4 місяці тому
The history of Visual Studio Code's WORST theme
Is Python 4 ON THE WAY?! (April Fools)
Переглядів 3,2 тис.4 місяці тому
Is Python 4 ON THE WAY?! (April Fools)
Creating, renaming, and deleting branches - Git Good
Переглядів 4574 місяці тому
Creating, renaming, and deleting branches - Git Good
7 COOL Python Easter eggs
Переглядів 2,1 тис.4 місяці тому
7 COOL Python Easter eggs
Ignoring files with .gitignore - Git Good
Переглядів 5825 місяців тому
Ignoring files with .gitignore - Git Good
Create an API in 20 MINUTES with FastAPI (2024 tutorial)
Переглядів 2 тис.5 місяців тому
Create an API in 20 MINUTES with FastAPI (2024 tutorial)
Pushing to and pulling from the remote - Git Good
Переглядів 4885 місяців тому
Pushing to and pulling from the remote - Git Good

КОМЕНТАРІ

  • @Lege19
    @Lege19 29 хвилин тому

    Just because you can do something doesn’t mean you should. If you need to index an iterator, you shouldn’t be using one

  • @professorice4325
    @professorice4325 5 годин тому

    Why not just index using enumerate?

    • @Carberra
      @Carberra 5 годин тому

      You could do that, but you would need to iterate over the iterable yourself. Using islice, that much is taken care of for you.

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 9 годин тому

    Thank you 😊

  • @igorthelight
    @igorthelight 12 годин тому

    "Python is a very simple language!" Sure! ;-)

  • @ike654
    @ike654 День тому

    len(text.split()) ??

  • @SusiePerez-d7o
    @SusiePerez-d7o День тому

    Harris Scott Williams Donna Lopez Karen

  • @iwswordpress
    @iwswordpress День тому

    Thanks! Merits over PyTest's built in tmp, tmpdir builtin fixtures?

  • @bevintx5440
    @bevintx5440 2 дні тому

    Flush writes a fille’s buffers, emptying (flushing) the buffer. Normally you you would only do that to meet specific needs, such as capturing data that can’t be readily duplicated. Files are buffered at three levels: by Python, the system, and lastly internally on modern drives. Flushing a file in Python does NOT also flush the system or drive buffers. You can flush a file’s system buffers with the caveats that some circumstances could prevent that from occurring, and it doesn’t flush the drive’s internal buffers. Furthermore, flushing a file’s system buffers can be “costly”, because your script must wait for a physical write before continuing (worse on hard drives than SSDs). This is what the documentation says: “If you're starting with a buffered Python file object f, first do f. flush(), and then do os. sync(f. fileno()), to ensure that all internal buffers associated with fare written to disk.”

  • @VinceKully
    @VinceKully 2 дні тому

    what did you need to use descriptors for in the real world?

    • @Carberra
      @Carberra День тому

      I talk about it in my video on descriptors. It's basically a type-safe, lazy-evaluated config class.

  • @OrWeinstein
    @OrWeinstein 2 дні тому

    unittest has it built-in.

  • @Zhaxxy
    @Zhaxxy 2 дні тому

    1:02 "we dont have to import anything" then immediately imports temp file (i think you meant to say install anything but still funny)

    • @Carberra
      @Carberra 2 дні тому

      Pahaha, managed to miss that in recording AND editing 😅

  • @emreozbek6770
    @emreozbek6770 2 дні тому

    python --version doesn't detect it

  • @nuurnwui
    @nuurnwui 2 дні тому

    docs on w+b: "The mode parameter defaults to 'w+b' so that the file created can be read and written without being closed. Binary mode is used so that it behaves consistently on all platforms without regard for the data that is stored.". keep up the good work!

    • @Carberra
      @Carberra 2 дні тому

      Thanks for that explanation!

  • @dipeshsamrawat7957
    @dipeshsamrawat7957 2 дні тому

    Thank you 😊

  • @kalyanben10
    @kalyanben10 2 дні тому

    Please update the title to indicate that you are not rebuilding Sqlite but are just doing a code crafter exercise-and that, too, not completely! This is a useless video! Have some respect for yourself as a developer!

  • @Tferdz
    @Tferdz 4 дні тому

    max **2 = sqrt(max). Way prettier

  • @Lege19
    @Lege19 5 днів тому

    Your changing the return type so you’d still need to update some code, it might be better to add a flag that defaults to False and print the locals when it’s true

  • @kokop1107
    @kokop1107 5 днів тому

    Why are you literally java-ing python😭😭😭

  • @Anarkhea
    @Anarkhea 6 днів тому

    Shot in the dark here.. Does Svk say you anything 😂😂

  • @Keithfert490
    @Keithfert490 6 днів тому

    If you want the total, you can use the total method instead of summing over the values.

  • @xaviermandeng3141
    @xaviermandeng3141 7 днів тому

    Great video. The Validator example is immediately useful.

  • @jlp2011
    @jlp2011 7 днів тому

    you can have `__init__(self, type_ :Type : int, f_val : Callable|None = None)` on the descriptor class to make it more generic. i.e the descriptor can get all sorts of arguments telling it how to behave. Knowing how to use that makes even more useful.

  • @knut-olaihelgesen3608
    @knut-olaihelgesen3608 7 днів тому

    If summing the word count only, don't use Counter

  • @TimSavage-drummer
    @TimSavage-drummer 7 днів тому

    The annotation improvements are very welcome, been avoiding upgrading the annotation support in odin because of the strings. The annotation parser is already fairly complex.

  • @dhjerth
    @dhjerth 7 днів тому

    Amazing, I have never seen these in the wild. Couple of times I've used properties in convoluted ways where this would be better, config validation is a good example. I just want to be pedantic and nitpick that "value is not negative" is distinct from "value is positive" because the latter is ambiguous on whether 0 is included.

    • @Carberra
      @Carberra 7 днів тому

      Very true, thanks for pointing that out! "Value just be greater than 0" is probably least ambiguous, rids the need for positive and negative completely.

  • @JscWilson
    @JscWilson 7 днів тому

    I can't believe I've gone so long without learning about descriptiors. They do make a few things I've seen make sense. @Carberra a video on generics would be appreciated

    • @Carberra
      @Carberra 7 днів тому

      Yeah same here! I only learned them in anger recently myself haha. A genetics video is planned!

  • @jlp2011
    @jlp2011 7 днів тому

    If you want to cache an environ lookup, I’d try to get rid of `self` and maybe even cls. Handing it off to a static/class method would seem better. But yes descriptors are pretty awesome. The intro verbiage about them is scary and makes them seem complicated. They’re not. “Descriptor Guide” by Hettinger covers 90% of use cases in 2 easy pages.

  • @wrathofainz
    @wrathofainz 8 днів тому

    Seeing this makes me glad that I keep things simple. This python reminds me of C#, which is odd.

  • @greob
    @greob 8 днів тому

    Wow this is really useful for testing closure functions! Thanks for the video!

  • @brunorodriguesmaso
    @brunorodriguesmaso 8 днів тому

    looks like a "hack", feels like a "hack", it's a "hack" :P naaaa, I'll skip this 🤪

  • @stevecanny1583
    @stevecanny1583 8 днів тому

    @Carberra I don't expect I'll ever use this just this way, but I'm very glad to have seen you walk through it. Seeing how to use a simply written profiler to react to events and access frames is a very interesting capability I'm not at all sure I would have come across otherwise :) It definitely sheds some light on how profilers and debuggers must work under the hood. Thanks for doing this! :)

  • @dmitryutkin9864
    @dmitryutkin9864 9 днів тому

    Limitless python

  • @khanra17
    @khanra17 9 днів тому

    Aggh! You try to speak everything in one breath and start the sentence louder and become so quiter in the end !!! so annoying!!!!!!!!

    • @austinnar4494
      @austinnar4494 9 днів тому

      @@khanra17 hey man, there are like, way more constructive ways to give this feedback. This is extremely rude

    • @austinnar4494
      @austinnar4494 9 днів тому

      Also, I've seen you leave this same comment on multiple of his videos. That is, like, extremely weird lol

    • @Carberra
      @Carberra 9 днів тому

      @@austinnar4494 You're more on the ball than I am lmao, I didn't even realise!

    • @carrotmanmatt
      @carrotmanmatt 9 днів тому

      I find the way he explains concepts very clear compared to many other developer UA-camrs actually.

  • @Cojua
    @Cojua 9 днів тому

    If you ever need something like this, you must be doing something wrong in your code and it's time to rethink the problem

    • @Carberra
      @Carberra 9 днів тому

      I mean, I did say that in the video. You would only want to use this for testing.

    • @carrotmanmatt
      @carrotmanmatt 9 днів тому

      @@Carberra Its a really cool concept to see under the hood of Python, but I still think your doing something very wrong if you need to use this in production. If your function doesn't return anything and doesn't modify any outside state (an object passed in/global state) then what use does the function have? If your function modifies some global state that prevents you from testing normally then you're doing something wrong and need to refactor to pass in the objects you will modify. And finally if you are modifying an object passed in then you should be able to read the object's state from outside the function after it has run in the test. I really love your videos and the way you explain concepts, but I do think its a bit silly to say this has legitimate uses.

    • @jlp2011
      @jlp2011 8 днів тому

      ⁠@@carrotmanmatt ok, we get it. You don’t like it. Don’t use it. But for those of us who have a different viewpoint, this is a nice hacky thing to know about, if not to use. What if you have a function that has the signature you want, but it is not working? What if it is not your function and you want to observe its state? What if you keep your core simple and fix whatever weird bug is at hand, without introducing the permanent over-engineering you seem to prescribe, then yank it right back out? What if this technique rings a bell 2 years down when you are faced with a seemingly unrelated problem? Carberra only said not to use for real like 5 times.

    • @Carberra
      @Carberra 8 днів тому

      @carrotmanmatt I only came up with this because I had an actual use case for it! At work, we have a function that sets up the Sentry SDK. This function takes in a number of configuration options, but calls sentry_sdk.init, then returns None. It also contains a closure function (enclosed function?) that is then passed to the init function. There's no sense in refactoring as the function never needs to return anything, and the enclosed function never needs to be run by anything else. This decorator allowed me to test the internal state and make sure all the internal configuration bits and pieces were functioning properly, and it also allowed me to rip the enclosed function out and test that to make sure it worked properly (thankfully, I confirmed it all already worked as expected). While you shouldn't use it in production code, using it to _test_ production code without making unnecessary modifications that affect UX is a valid use case for it. (Appreciate your kind words about the channel generally as well! 😄)

    • @1vader
      @1vader 8 днів тому

      @@Carberra Still seems like a fairly odd use tbh. I feel like the cleaner solution would be to mock sentry_sdk.init.

  • @austinnar4494
    @austinnar4494 9 днів тому

    The reason f_locals needs to be nonlocal is because the variable *name* is being assigned to. You can always access variables from the enclosing scope, but you cannot assign a new value to that name. You can, however, mutate variables without marking them nonlocal; if instead, you mutated that f_locals dict by using f_locals.update you could have omitted the nonlocal tag

    • @Carberra
      @Carberra 9 днів тому

      Yeah that's what I was _trying_ to say and failed miserably lmao, thanks for that explanation!

    • @austinnar4494
      @austinnar4494 9 днів тому

      @@Carberra haha np, and yeah I assumed you knew why, mostly just a hard thing to explain on the fly

    • @Carberra
      @Carberra 9 днів тому

      Yeah it deffo can be haha. This is why I'm glad viewers leave comments like this, always good to have corrections/additional context! For myself as well as everyone else.

    • @nurmr
      @nurmr 7 днів тому

      Yea, before the nonlocal keyword existed this was annoying. I typically did something like val = [None], then inside the function: val[0] = thing_I_want, and then outside refer to val[0] to get the value back. nonlocal makes this so much easier.

  • @davidmurphy563
    @davidmurphy563 9 днів тому

    1:14 Not even close! :)) error-TOSSED-hen-ease Stressed syllable in caps. Not phenetics, pronounce as English words.

    • @davidmurphy563
      @davidmurphy563 9 днів тому

      Fun fact: Eratosthenes was the first person to measure the size of the planet and got it to within 10% of the actual answer. Although we aren't totally certain on the length of the units he was using, the presumption is the Oympic Stade which is accurate. Regardless, the approach was sound. He put an upright stick in Alexandria at noon during solstice when the was no shadow and another in Syene at the same time where there was a shadow. Then by knowing the distance between the two spots and the angle of the shadow in Syene he could calculate the circumference of the planet with some basic trig. The dude rocked.

    • @Carberra
      @Carberra 9 днів тому

      Absolute rip @ my pronunciation lmao. Pretty cool fact though! Guy seemed like he did quite a lot, amazed I'd never heard of him before now!

    • @davidmurphy563
      @davidmurphy563 9 днів тому

      ​@@Carberra Was a cool dude. Chief librarian to Great Library of Alexandria. Worked out the Earth's axial tilt. Made the first world map. He invented the field of chronology, estimating dates between events like the battles and whatnot. He found the distance to the sun and moon - I'm not sure off hand how accurate he got with those. Rather amusingly and famously, there's an ancient text from 190BC or thereabouts mocking him for always only being second best at everything.

    • @Carberra
      @Carberra 9 днів тому

      Bloody hell, we have a lot to thank him for! I guess as far as that text is concerned at least, he also invented how to a jack of all trades, master at none 😅

    • @davidmurphy563
      @davidmurphy563 9 днів тому

      @@Carberra Yeah, I'm filing his CV under "rubbish at python" and moving on. ;) Been a long time since I watched it but I'm pretty sure Carl Sagan covered him in Cosmos.