Skip to content

24th Week of 2022

Coding

  • New: Themes.

    Vuetify comes with two themes pre-installed, light and dark. To set the default theme of your application, use the defaultTheme option.

    File: src/plugins/vuetify.js

    import { createApp } from 'vue'
    import { createVuetify } from 'vuetify'
    
    export default createVuetify({
      theme: {
        defaultTheme: 'dark'
      }
    })
    

    Adding new themes is as easy as defining a new property in the theme.themes object. A theme is a collection of colors and options that change the overall look and feel of your application. One of these options designates the theme as being either a light or dark variation. This makes it possible for Vuetify to implement Material Design concepts such as elevated surfaces having a lighter overlay color the higher up they are.

    File: src/plugins/vuetify.js

    import { createApp } from 'vue'
    import { createVuetify, ThemeDefinition } from 'vuetify'
    
    export default createVuetify({
      theme: {
        defaultTheme: 'myCustomLightTheme',
        themes: {
          myCustomLightTheme: {
            dark: false,
            colors: {
              background: '#FFFFFF',
              surface: '#FFFFFF',
              primary: '#510560',
              'primary-darken-1': '#3700B3',
              secondary: '#03DAC6',
              'secondary-darken-1': '#018786',
              error: '#B00020',
              info: '#2196F3',
              success: '#4CAF50',
              warning: '#FB8C00',
            }
          }
        }
      }
    })
    

    To dynamically change theme during runtime.

    <template>
      <v-app>
        <v-btn @click="toggleTheme">toggle theme</v-btn>
        ...
      </v-app>
    </template>
    
    <script>
    import { useTheme } from 'vuetify'
    
    export default {
      setup () {
        const theme = useTheme()
    
        return {
          theme,
          toggleTheme: () => theme.global.name.value = theme.global.current.value.dark ? 'light' : 'dark'
        }
      }
    }
    </script>
    

    Most components support the theme prop. When used, a new context is created for that specific component and all of its children. In the following example, the v-btn uses the dark theme applied by its parent v-card.

    <template>
      <v-app>
        <v-card theme="dark">
          <!-- button uses dark theme -->
          <v-btn>foo</v-btn>
        </v-card>
      </v-app>
    </template>
    
  • New: Add more elements.

Python

  • New: Generators.

    Generator functions are a special kind of function that return a lazy iterator. These are objects that you can loop over like a list. However, unlike lists, lazy iterators do not store their contents in memory.

    An example would be an infinite sequence generator

    def infinite_sequence():
        num = 0
        while True:
            yield num
            num += 1
    

    You can use it as a list:

    for i in infinite_sequence():
    ...     print(i, end=" ")
    ...
    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
    30 31 32 33 34 35 36 37 38 39 40 41 42
    [...]
    

    Instead of using a for loop, you can also call next() on the generator object directly. This is especially useful for testing a generator in the console:.

    >>> gen = infinite_sequence()
    >>> next(gen)
    0
    >>> next(gen)
    1
    >>> next(gen)
    2
    >>> next(gen)
    3
    

BeautifulSoup

  • New: Modifying the tree.

    PageElement.replace_with() removes a tag or string from the tree, and replaces it with the tag or string of your choice:

    markup = '<a href="http://example.com/">I linked to <i>example.com</i></a>'
    soup = BeautifulSoup(markup)
    a_tag = soup.a
    
    new_tag = soup.new_tag("b")
    new_tag.string = "example.net"
    a_tag.i.replace_with(new_tag)
    
    a_tag
    

    Sometimes it doesn't work. If it doesn't use:

    +a_tag.clear()
    a_tag.append(new_tag)
    

Boto3

Feedparser

  • New: Parse a feed from a string.

    >>> import feedparser
    >>> rawdata = """<rss version="2.0">
    <channel>
    <title>Sample Feed</title>
    </channel>
    </rss>"""
    >>> d = feedparser.parse(rawdata)
    >>> d['feed']['title']
    u'Sample Feed'
    

Python Snippets

  • New: Fix SIM113 Use enumerate.

    Use enumerate to get a running number over an iterable.

    ```python idx = 0 for el in iterable: ... idx += 1

    for idx, el in enumerate(iterable): ...