Droptica: Layout Builder Customization. Creating Custom Layout in Drupal

Planet Drupal - Fri, 2022-12-02 08:14

Layout Builder allows quickly creating website layouts from pre-built components added to sections. By default, Drupal provides four types of sections: one, two, three, and four columns. These columns have a predefined behavior the editors have no control over. Drupal offers the ability to create our own types of sections, so we can tailor them to fit our project. This is the process we'll show you in our article.

How to create a custom section in Layout Builder?

The first and most important step is to define the goals and thus, the list of functionalities that the section should provide. Then it's worth breaking the functionalities into small tasks that can be done in a specific time. The aim of our section will be to provide the ability to add classes to the main section wrapper and to individual regions.

As our base we'll use the template available in the Drupal Layout Builder module, which is the one that's used in the sections available with the module installation. Our task list should include:

  • creating a custom module,
  • the definition of a section in the layouts.yml file,
  • the definition of a template for our sections and the plugin in which we'll embed the logic of adding our classes.
Creating a new module in Drupal

We have to create a standard *.info.yml file, as in every module. For a detailed description please refer to the documentation on Drupal.org.

# filename: custom_layout_builder.info.yml name: Custom Layout Builder sections description: Functionality which extends Layout Builder core_version_requirement: ^9 type: module package: custom dependencies: - drupal:layout_builder

We know what the purpose of the module is because we've defined the required functionality. Therefore, already at this stage, we're sure that the list of dependencies should include at least the Layout Builder module. After defining the info.yml file, it's worth clearing the cache and checking if the module appears on the list. To do this, let’s go to the modules view and search for the module by the title or machine name. We should see our module with a list of required dependencies.


As we can clearly see, even though we've provided a dependency to the Layout Builder module only, their list is a bit longer. This is because the Layout Builder module has its own dependency list and it’s inherited by our module.


At this stage, it's worth considering the mental health of other developers (or yours, if you come back to this code after a few months) and starting to build its documentation. It's worth beginning with the implementation of the hook hook_help().


It's also a good idea to create a README.md file and keep it updated.

Section registration using *.layouts.yml

In order to register a new section, the easiest way is to add the *.layouts.yml file (where * is the machine name of our module). The file should be added in the main folder of the module, where we added the *.info.yml file.

Let's start with defining one section:

# filename: ustom_layout_builder.layouts.yml layout_custom_one_column: # Section main key label: '[CUSTOM] One column' # Section title category: 'Custom layouts' path: layouts/custom_onecol_section # Relative path from the template template: layout--custom-onecol-section # Template name default_region: first icon_map: - [first] regions: # Regions table first: # Region machine name label: First # Region title

After the configuration, when adding sections, we should be able to see our newly defined section.

  Section template defining

In order to be able to add a section, we still need to add the template whose name and path we've specified. In our case, we need to create the layouts/custom_onecol_section folder inside of which the layout--custom-onecol-section.html.twig file must be placed.

By default, the template will have access to four variables: content, attributes, region_attributes, and settings. If we don't put any block in the section, the content variable will return false after being cast to a boolean value. We can take advantage of this behavior to avoid displaying empty HTML tags. Inside the content variable, we'll find the keys corresponding to each defined region, and inside these regions are the blocks we've added. In the content variable, we'll only find the first key, because that's the only one we've defined. The behavior of content.first when casting to a boolean value is analogous to the behavior of the content variable. We'll use this to not display empty tags.

# filename: layout--custom-onecol-section.html.twig {# /** * @file * Default implementation for a custom layout onecol section. * * Available variables: * - content: The content for this layout. * - attributes: HTML attributes for the layout . * - region_attributes: HTML attributes for the region . * - settings: An array of configured settings for the layout. * * @ingroup themeable */ #} {% if content %} {% if content.first %} {{ content.first }} {% endif %} {% endif %}

After adding the template, we should be able to easily add our section:

Defining the Layout plugin

From the end user's perspective, we haven't done anything so far, because the content editor will only see the new section title with the big [CUSTOM] prefix. This is because the section we've added works identically to the default one, provided with the Layout Builder module (with a small exception: our implementation doesn't add any classes). To change its behavior, we have to implement a new layout plugin.

Base class framework

The class should be in the src/Plugin/Layout folder. It'll be generic enough so that it can be used for any number of regions. The Drupal\Core\Layout\LayoutDefault class contains many base methods and implements the needed interfaces. As not to reinvent the wheel, we can expand it in our class.

# filename: CustomLayoutClassBase.php <?php namespace Drupal\custom_layout_builder\Plugin\Layout; use Drupal\Core\Layout\LayoutDefault; /** * Base class of our custom layouts with configurable HTML classes. * * @internal * Plugin classes are internal. */ class CustomLayoutClassBase extends LayoutDefault { } Adding configuration options to a base class

One of the requirements is the possibility to select a class for the tag wrapping the section's regions. To achieve this, we have to first override the defaultConfiguration method and add a new configuration option to it.

/** * {@inheritdoc} */ public function defaultConfiguration() { $configuration = parent::defaultConfiguration(); return $configuration + [ 'wrapper_classes' => '', ]; }

Then we should add the ability to specify a value for this configuration option. We can do this by overriding the buildConfigurationForm and submitConfigurationForm methods.

/** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form['wrapper_classes'] = [ '#type' => 'textfield', '#title' => $this->t('Wrapper extra classes'), '#default_value' => $this->configuration['wrapper_classes'], '#description' => $this->t('Extra wrapper classes. Type as many as you want but remember to separate them by using a single space character.'), ]; return parent::buildConfigurationForm($form, $form_state); } /** * {@inheritdoc} */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { parent::submitConfigurationForm($form, $form_state); $this->configuration['wrapper_classes'] = $form_state->getValue('wrapper_classes'); }

If there's a need to add form validation, it can be done by overwriting the validateConfigurationForm method. We recommend implementing validation for this field, as classes should comply with the Drupal Community standard. The Html::getClass() method may come in handy in this case.

Using configuration to build sections

The render array is created in the build method, and that's what we're going to overwrite now. If you remember the contents of the template we've added, you probably already know that we add classes to the attributes object.

/** * {@inheritdoc} */ public function build(array $regions): array { $build = parent::build($regions); $wrapper_classes = explode(' ', (string) $this->configuration['wrapper_classes']); $build['#attributes']['class'] = [...$wrapper_classes]; return $build; } Using the base to create a Layout Plugin

Our class is ready, it's time to use it in a section. To do this, go back to the *.layouts.yml file to declare a new plugin. This is done by specifying the full namespace of the class under the class key.

# filename: custom_layout_builder.layouts.yml layout_custom_one_column: label: '[CUSTOM] One column' category: 'Custom layouts' path: layouts/custom_onecol_section template: layout--custom-onecol-section class: '\Drupal\custom_layout_builder\Plugin\Layout\CustomOneColLayout' default_region: first icon_map: - [first] regions: first: label: First

After introducing the above changes, you may notice that the section form has a new field and that the classes entered in that field are in the correct place in HTML.

Adding the option to select classes for regions We can already define a class list for the wrapper element in our section. It's time to think about how to create the logic responsible for adding classes to individual sections of our layout. We should take into consideration the extensibility of our base class. That's why we recommend basing the logic of determining and accessing regions on the basis of the getRegionNames() method of the LayoutDefinition class.

1. First, we add one field to our form for each region:

/** * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form['wrapper_classes'] = [ '#type' => 'textfield', '#title' => $this->t('Wrapper extra classes'), '#default_value' => $this->configuration['wrapper_classes'], '#description' => $this->t('Extra wrapper classes. Type as many as you want but remember to separate them by using a single space character.'), ]; foreach ($this->getPluginDefinition()->getRegionNames() as $region_name) { $form['region_classes'][$region_name] = [ '#type' => 'textfield', '#title' => $this->t('Extra classes for @region_name region', [ '@region_name' => $region_name, ]), '#default_value' => $this->configuration['region_classes'][$region_name], '#description' => $this->t('Extra classes for the @region_name region wrapper. Type as many as you want but remember to separate them by using a single space character.', [ '@region_name' => $region_name, ]), ]; } return parent::buildConfigurationForm($form, $form_state); }

2. We use a similar loop to write the value:

/** * {@inheritdoc} */ public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { parent::submitConfigurationForm($form, $form_state); $this->configuration['wrapper_classes'] = $form_state->getValue('wrapper_classes'); foreach ($this->getPluginDefinition()->getRegionNames() as $region_name) { $this->configuration['region_classes'][$region_name] = $form_state->getValue(['region_classes', $region_name], ''); } }

3. The last step will be overriding the build method and embedding our classes in the appropriate Attributes class object.

/** * {@inheritdoc} */ public function build(array $regions): array { $build = parent::build($regions); $wrapper_classes = explode(' ', (string) $this->configuration['wrapper_classes']); $build['#attributes']['class'] = [...$wrapper_classes]; foreach (array_keys($regions) as $region_name) { $region_classes = explode(' ', (string) $this->configuration['region_classes'][$region_name]); $build[$region_name]['#attributes']['class'] = [...$region_classes]; } return $build; }

After our latest changes, we should see a new Extra classes for first region field where we can provide a list of classes we want to use.

Keep in mind that the region will only appear if it's not empty. That's why we've added a test block containing the node's title. Let's see if classes are visible in HTML.

Creating different variants of sections The code was written in such a generic way that adding a section with a different number of regions requires only the definition of the region and template from us. Let's add then a new section containing two regions.

First, we add the definition:

# filename: custom_layout_builder.layouts.yml layout_custom_one_column: label: '[CUSTOM] One column' category: 'Custom layouts' path: layouts/custom_onecol_section template: layout--custom-onecol-section class: '\Drupal\custom_layout_builder\Plugin\Layout\CustomLayoutClassBase' default_region: first icon_map: - [first] regions: first: label: First layout_custom_two_columns: label: '[CUSTOM] Two columns' category: 'Custom layouts' path: layouts/custom_twocol_section template: layout--custom-twocol-section class: '\Drupal\custom_layout_builder\Plugin\Layout\CustomLayoutClassBase' default_region: first icon_map: - [first, second] regions: first: label: First second: label: Second

And then we prepare the template:

# filename: layout--custom-twocol-section.html.twig {# /** * @file * Default implementation for a custom layout onecol section. * * Available variables: * - content: The content for this layout. * - attributes: HTML attributes for the layout . * - region_attributes: HTML attributes for the region . * - settings: An array of configured settings for the layout. * * @ingroup themeable */ #} {% if content %} {% if content.first %} {{ content.first }} {% endif %} {% if content.second %} {{ content.second }} {% endif %} {% endif %}


The section should be available now.


The configuration form should automatically adjust to the number of regions.

After configuring the form and adding test data, we can see the result of our operation in HTML.


The module created in this tutorial is available on our GitHub account.

Layout Builder customization - summary

Layout Builder is a great tool whose API allows for full freedom. As always with Drupal, if you can dream it, you can build it. The example shown in this article is only a small part of what can be achieved. If you're interested in the wider use of the Layout Builder API, it's worth reading about the Bootstrap Layout Builder module.

Do you need custom settings in your system? Check out how we can help you as part of our services related to Drupal development.

Categories: FLOSS Project Planets

Web Review, Week 2022-48

Planet KDE - Fri, 2022-12-02 07:22

Let’s go for my web review for the week 2022-48.

osquery | Easily ask questions about your Linux, Windows, and macOS infrastructure

Tags: tech, monitoring

This looks like an interesting OS level monitoring solution.


WebAssembly: Go vs Rust vs AssemblyScript :: Ecostack — a developer blog

Tags: tech, webassembly, performance

Little simple benchmark of WebAssembly performances for the most common languages found there. Careful to the payload size though.


Using Rust at a startup: A cautionary tale | by Matt Welsh | Nov, 2022 | Medium

Tags: tech, programming, rust, architecture

Don’t believe claims about Rust (or any other options in fact) being a language for universal use. It has a few spaces where it shines and others where it’ll be a drag. Picking the right language and stack is a multi-factor decision process where the technical advantages of the language itself say less than half of the story.


I am disappointed by dynamic typing • Buttondown

Tags: tech, type-systems, metaprogramming

Interesting take about what could make dynamic typing truly shine if it got all the way to runtime manipulation in a consistent manner. We’re far from it though.


Git Notes: git’s coolest, most unloved­ feature - Tyler Cipriani

Tags: tech, git

Obscure feature definitely but we’re happy it’s there… maybe one day it’ll indeed allow to have much more independence from the code forges.


I/O is no longer the bottleneck

Tags: tech, performance

Definitely this, we have to stop pointing disk I/O so much for performance issues. This is just not really slow anymore. Obviously network is a different story.


Falsehoods programmers believe about undefined behavior

Tags: tech, compiler, c, c++, rust

Undefined behavior do exist and well… they’re really undefined, don’t make any assumption about them.


Cache invalidation really is one of the hardest problems in computer science – Surfing Complexity

Tags: tech, performance, multithreading

Nice summary on the false sharing problem with caches and how it can impact your performances in multithreaded contexts.


Recognizing patterns in memory // TimDbg

Tags: tech, debugging, memory

Interesting set of memory patterns. Didn’t know all of them, some are definitely useful and I already use, I’ll try to look for the others next time I need to.


Massively increase your productivity on personal projects with comprehensive documentation and automated tests

Tags: tech, git, project-management, maintenance

Nice list of things to keep in mind when working on projects, even small personal ones. This greatly improve maintainability in the long run.


Why writing by hand is still the best way to retain information - Stack Overflow Blog

Tags: tech, low-tech, note-taking, book

There’s definitely a tension between something which you can organize and search easily (by typing) and something you can remember better (by hand writing). That’s why I can’t get rid of hand written notes completely, I practice a mix of both depending on the use.


Bye for now!

Categories: FLOSS Project Planets

Real Python: The Real Python Podcast – Episode #135: Preparing Data to Measure True Machine Learning Model Performance

Planet Python - Fri, 2022-12-02 07:00

How do you prepare a dataset for machine learning (ML)? How do you go beyond cleaning the data and move toward measuring how the model performs? This week on the show, Jodie Burchell, developer advocate for data science at JetBrains, returns to talk about strategies for better ML model performance.

[ Improve Your Python With 🐍 Python Tricks 💌 – Get a short & sweet Python Trick delivered to your inbox every couple of days. >> Click here to learn more and see examples ]

Categories: FLOSS Project Planets

Lucas Cimon: Animated one-page-dungeon : Escape of the Torment

Planet Python - Fri, 2022-12-02 06:56

Last week, while translating John Harper's micro-TTRPG World of Dungeons: Turbo Breakers, I discovered the wonderful world of one page dungeons, starting with Michael Prescott splendid production at trilemma.com and also the yearly One Page Dungeon Context.

While crawling through the OPDC 2021 entries, I discovered a great map …

Categories: FLOSS Project Planets

Andrea Grandi: Ignoring hosts with python vcr when writing tests with pytest and generating cassettes

Planet Python - Fri, 2022-12-02 05:30

How to ignore hosts with python vcr when writing tests with pytest and generating cassettes

Categories: FLOSS Project Planets

Junichi Uekawa: Already December.

Planet Debian - Fri, 2022-12-02 05:13
Already December. Things changed a lot. Six months ago I was at home most of the time. I hope I can keep on going out for a while.

Categories: FLOSS Project Planets

KDE's End of Year Fundraiser is Live

Planet KDE - Fri, 2022-12-02 04:29
You Make KDE Possible

KDE's End of Year Fundraiser is officially live! Your donations will help us reach our goals, support our community, fund our events, and show the world how everybody can benefit from KDE software.

Today we have the ambitious goal of raising 20,000€ for KDE. Your donation allows KDE to continue developing the spectacular Plasma desktop and all the apps you need for education, productivity, and creative work. Here are some of the things we have managed to do over the last year thanks to the generosity of donors:

Reaching the World

  • We have welcomed 2785+ people worldwide who have contributed code, art, translations and more.
  • We added/maintained support for 40+ languages for apps and frameworks.
  • We organized and attended 18 community events/sprints.

Building the Products

  • We hosted 1000+ projects and repositories.
  • We continued developing 260+ applications and addons.
  • We pushed out 11+ updates for KDE's Plasma desktop and related environments, such as Plasma Mobile and Plasma Big Screen and applications.
  • We supported 12 hardware platforms.
  • We continued to develop 83 frameworks.

The work of KDE is made possible thanks to the contributions from KDE Community members, donors and corporations that support us. Every individual counts, and every commitment, large or small, is a commitment to Free Software. Head to the KDE's End of Year fundraiser page and donate now.

Want to help more? Join KDE and contribute to building the future of KDE.


Categories: FLOSS Project Planets

Talk Python to Me: #392: Data Science from the Command Line

Planet Python - Fri, 2022-12-02 03:00
When you think data science, Jupyter notebooks and associated tools probably come to mind. But I want to broaden your toolset a bit and encourage you to look around at other tools that are literally at your fingertips. The terminal and shell command line tools. <br/> <br/> On this episode, you'll meed Jeroen Janssens. He wrote the book Data Science on The Command Line Book and there are a bunch of fun and useful small utilities that will make your life simpler that you can run immediately in the terminal. For example, you can query a CSV file with SQL right from the command line.<br/> <br/> <strong>Links from the show</strong><br/> <br/> <div><b>Jeroen's Website</b>: <a href="https://jeroenjanssens.com" target="_blank" rel="noopener">jeroenjanssens.com</a><br/> <b>Jeroen on LinkedIn</b>: <a href="https://www.linkedin.com/in/jeroenjanssens" target="_blank" rel="noopener">linkedin.com</a><br/> <b>Jeroen cohort-based course, Embrace the Command Line. Listeners can use coupon code TALKPYTHON20 for a 20% discount</b>: <a href="https://maven.com/data-science-workshops/embrace-the-command-line" target="_blank" rel="noopener">maven.com</a><br/> <br/> <b>Data Science on The Command Line Book</b>: <a href="https://datascienceatthecommandline.com/2e/index.html" target="_blank" rel="noopener">datascienceatthecommandline.com</a><br/> <b>McFly Shell History Tool</b>: <a href="https://github.com/cantino/mcfly" target="_blank" rel="noopener">github.com</a><br/> <b>Explain Shell</b>: <a href="https://explainshell.com" target="_blank" rel="noopener">explainshell.com</a><br/> <b>CSVKit</b>: <a href="https://csvkit.readthedocs.io/en/latest/index.html" target="_blank" rel="noopener">csvkit.readthedocs.io</a><br/> <b>sql2csv</b>: <a href="https://csvkit.readthedocs.io/en/latest/scripts/sql2csv.html" target="_blank" rel="noopener">csvkit.readthedocs.io</a><br/> <b>pipx</b>: <a href="https://github.com/pypa/pipx" target="_blank" rel="noopener">github.com</a><br/> <b>PyProject.toml to add entry points</b>: <a href="https://github.com/mikeckennedy/twitter-archive-parser/blob/installable/pyproject.toml" target="_blank" rel="noopener">github.com</a><br/> <b>rich-cli</b>: <a href="https://github.com/Textualize/rich-cli" target="_blank" rel="noopener">github.com</a><br/> <b>Typer</b>: <a href="https://typer.tiangolo.com/typer-cli/" target="_blank" rel="noopener">typer.tiangolo.com</a><br/> <b>FasD</b>: <a href="https://github.com/clvv/fasd" target="_blank" rel="noopener">github.com</a><br/> <b>Nerd Fonts</b>: <a href="https://www.nerdfonts.com/font-downloads" target="_blank" rel="noopener">nerdfonts.com</a><br/> <b>Xonsh</b>: <a href="https://xon.sh" target="_blank" rel="noopener">xon.sh</a><br/> <b>iTerm</b>: <a href="https://iterm2.com" target="_blank" rel="noopener">iterm2.com</a><br/> <b>Windows Terminal</b>: <a href="https://apps.microsoft.com/store/detail/windows-terminal/9N0DX20HK701?hl=en-us&gl=us" target="_blank" rel="noopener">microsoft.com</a><br/> <b>ohmyposh</b>: <a href="https://ohmyposh.dev" target="_blank" rel="noopener">ohmyposh.dev</a><br/> <b>ohmyz</b>: <a href="https://ohmyz.sh" target="_blank" rel="noopener">ohmyz.sh</a><br/> <b>Watch this episode on YouTube</b>: <a href="https://www.youtube.com/watch?v=wNJ3tTPkuzY" target="_blank" rel="noopener">youtube.com</a><br/> <b>Episode transcripts</b>: <a href="https://talkpython.fm/episodes/transcript/392/data-science-from-the-command-line" target="_blank" rel="noopener">talkpython.fm</a><br/> <br/> <b>--- Stay in touch with us ---</b><br/> <b>Subscribe to us on YouTube</b>: <a href="https://talkpython.fm/youtube" target="_blank" rel="noopener">youtube.com</a><br/> <b>Follow Talk Python on Mastodon</b>: <a href="https://fosstodon.org/web/@talkpython" target="_blank" rel="noopener"><i class="fa-brands fa-mastodon"></i>talkpython</a><br/> <b>Follow Michael on Mastodon</b>: <a href="https://fosstodon.org/web/@mkennedy" target="_blank" rel="noopener"><i class="fa-brands fa-mastodon"></i>mkennedy</a><br/></div><br/> <strong>Sponsors</strong><br/> <a href='https://talkpython.fm/sentry'>Sentry Error Monitoring, Code TALKPYTHON</a><br> <a href='https://talkpython.fm/foundershub'>Microsoft</a><br> <a href='https://talkpython.fm/assemblyai'>AssemblyAI</a><br> <a href='https://talkpython.fm/training'>Talk Python Training</a>
Categories: FLOSS Project Planets

John Ludhi/nbshare.io: Understand Tensors With Numpy

Planet Python - Fri, 2022-12-02 02:39
Understand Tensors With Numpy

In this notebook, I will explain tensors with Python Numpy. For numpy refresher please checkout following resource...

In [1]: import numpy as np

Before we delve in to tensors. We need to understand first what is Scalars, Vectors, Arrays and Matrices.

Scalar - Scalar is just a number. Example number 55 is a scalar. Scalar has no dimensions.
Vector - Vector is one dimensional array or scalar with orientation (i.e. horizontal or vertical). Vector can be 1,2 or n dimensional.
Array - Array is a computer language term used in data structures. Array is a collection of similar data elements.
Matrix - Matrix is a mathematical term. It is a 2D array of numbers represented in rows and columns.

What is Vector

Example of 3 dimensional Vector is shown below.

In [2]: threeDVector = np.array([1,2,3]) In [3]: threeDVector Out[3]: array([1, 2, 3]) In [4]: threeDVector.shape Out[4]: (3,) What is Matrix

Matrix is 2 dimensional array.

In [5]: np.array([[1,2,3],[4,5,6]]) Out[5]: array([[1, 2, 3], [4, 5, 6]]) What is Tensor

Tensor is a mathematical object with more than two dimensions. The number of dimensions of an array defines the order of a Tensor. For example a scalar is 0 order tensor. Similarly vector is a 1 order tensor. A 2D matrix is 2nd order Tensor so on and so forth.

Below is an example of vector which is 1 order tensor.

In [6]: np.array([1,2,3]) Out[6]: array([1, 2, 3])

Let us define a two dimensional tensor.

In [7]: twodTensor = np.arange(36).reshape(6,6) In [8]: twodTensor Out[8]: array([[ 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]])

Let us access the ist row of above tensor

In [9]: twodTensor[0] Out[9]: array([0, 1, 2, 3, 4, 5]) In [10]: twodTensor.shape Out[10]: (6, 6)

Three dimensional Tensor example is shown below...

In [11]: threedTensor = np.arange(36).reshape(4,3,3) In [12]: threedTensor Out[12]: array([[[ 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]]])

Note the 3 enclosing brackets - array[[[]]] which indicates it is 3 dimensional tensor.

Add new dimension or axis to a tensor. In [13]: t3t = np.arange(36).reshape(4,3,3)

Let us make our 3 dimension tensor to a 4 dimension tensor.

In [14]: t4t = t3t[np.newaxis,:,:]

Note the 4 enclosing brackets in the below array.

In [15]: t4t Out[15]: array([[[[ 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]]]])

Note our 4th dimension has only 1 element (the 3x3 array). Let us see that using tensor.shape

In [16]: t4t.shape Out[16]: (1, 4, 3, 3) In [17]: t4t[0] #4th dimension Out[17]: array([[[ 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]]]) Arithmetic with Tensors

Tensors Arithemetic is mathematical operations on vector and matrix.

Example - Find the dot product of two 3 dimensional vectors, we can use np.dot

In [18]: array1 = np.array([4,4,3]) array2 = np.array([2,2,3]) In [19]: array1.shape Out[19]: (3,) In [20]: array2.shape Out[20]: (3,) In [21]: np.dot(array1,array2) Out[21]: 25

Dot product two matrix is shown below.

In [22]: array1 = np.array([[1,2,3],[4,4,3]]) array2 = np.array([[2,2,3],[1,2,3]]) In [23]: print(array1) print("\n") print(array2) [[1 2 3] [4 4 3]] [[2 2 3] [1 2 3]] In [24]: np.dot(array1,array2) #this will throw error because row of first matrix is not equal to column of second matrix - Refer matrix multiplication fundamentals --------------------------------------------------------------------------- ValueError Traceback (most recent call last) Input In [24], in <cell line: 1>() ----> 1 np.dot(array1,array2) File <__array_function__ internals>:180, in dot(*args, **kwargs) ValueError: shapes (2,3) and (2,3) not aligned: 3 (dim 1) != 2 (dim 0) In [25]: array1 = np.array([[1,2,3],[4,4,3],[1,1,1]]) array2 = np.array([[2,2,3],[1,2,3],[1,2,3]])

Dot product for above matrices will work because both the arrays are 3x3 matrices.

In [26]: np.dot(array1,array2) Out[26]: array([[ 7, 12, 18], [15, 22, 33], [ 4, 6, 9]])
Categories: FLOSS Project Planets

Help KDE hire more people!

Planet KDE - Thu, 2022-12-01 23:34

KDE’s 2022 year-end fundraiser is now live! Please go donate if you can.

It’s been several years since we did a fundraiser at the end of the year, and we’re going to be more on the ball about this going forward, given how much the KDE e.V. is expanding hiring. This year’s fundraiser sets the fairly modest goal of 20k €, which will help offset the cost of some of that hiring.

But of course… there’s no reason not to exceed the goal! The more money raised, the more contributors the KDE e.V. can hire directly, effecting the kind of professionalization needed to take KDE to the next level! We have big plans and we can’t do it without your help!

I know I’ve asked for a lot of donations recently, so if you’re feeling tapped out, this is a good time to go nudge your friends and family members who you’ve converted to KDE over the years! Help KDE challenge the big dogs and win!

Categories: FLOSS Project Planets

Paul Wise: FLOSS Activities November 2022

Planet Debian - Thu, 2022-12-01 21:13

This month I didn't have any particular focus. I just worked on issues in my info bubble.

Changes Issues Review Administration
  • Debian IRC: setup topic/entrymsg to redirect OFTC #debian-jp to the Debian Japan IRC server, cleaned up #debianppc ChanServ metadata
  • Debian wiki: unblock IP addresses, approve domains, approve accounts
Communication Sponsors

The libemail-outlook-message-perl, purple-discord, sptag, celery work was sponsored. All other work was done on a volunteer basis.

Categories: FLOSS Project Planets

Matt Layman: Learn Python By Example - Ghost Gobble Arcade Game

Planet Python - Thu, 2022-12-01 19:00
Learn Python By Example shows a simple Python exercise from Exercism. This problem illustrates booleans in Python.
Categories: FLOSS Project Planets

Oomph Insights: Oomph Earns Acquia Drupal Cloud Practice Certification

Planet Drupal - Thu, 2022-12-01 19:00
We are thrilled to share that Oomph has been recognized as an Acquia Certified Drupal Cloud Practice for completing Acquia’s rigorous evaluation program, which recognizes the highest standards of technical delivery on the platform. To earn Drupal Cloud Practice Certification, Acquia partners must meet a stringent set of technical criteria. These requirements include a core team of Acquia certified developers, significant hands-on experience delivering Acquia Drupal Cloud products to clients, and a meticulous company review with Acquia partner specialists.  “I am incredibly proud that our team…
Categories: FLOSS Project Planets

Reproducible Builds (diffoscope): diffoscope 228 released

Planet Debian - Thu, 2022-12-01 19:00

The diffoscope maintainers are pleased to announce the release of diffoscope version 228. This version includes the following changes:

[ FC Stegerman ] * As an optimisation, don't run apktool if no differences are detected before the signing block. (Closes: reproducible-builds/diffoscope!105) [ Chris Lamb ] * Support both the python3-progressbar and python3-progressbar2 Debian packages, two modules providing the "progressbar" Python module. (Closes: reproducible-builds/diffoscope#323) * Ensure we recommend apksigcopier. (Re: reproducible-builds/diffoscope!105) * Make the code clearer around generating the Debian substvars and tidy generation of os_list. * Update copyright years.

You find out more by visiting the project homepage.

Categories: FLOSS Project Planets

Dirk Eddelbuettel: spdl 0.0.2 on CRAN: First Update

Planet Debian - Thu, 2022-12-01 17:09

A first update to the recently-released package spdl is now om CRAN. The key focus of spdl is a offering the same interface from both R and C++ for logging by relying on spdlog via my RcppSpdlog package.

This release exposes simple helpers fmt() (to format text according to the included fmt library) and cat() which formats and prints.

The very short NEWS entry for this release follows.

Changes in spld version 0.0.2 (2022-12-01)
  • Helper functions fmt() and cat() have been added

Courtesy of my CRANberries, there is also a diffstat report. More detailed information is on the spdl page.

If you like this or other open-source work I do, you can sponsor me at GitHub.

This post by Dirk Eddelbuettel originated on his Thinking inside the box blog. Please report excessive re-aggregation in third-party for-profit settings.

Categories: FLOSS Project Planets

PyCharm: PyCharm 2022.3 Is Out!

Planet Python - Thu, 2022-12-01 15:04

PyCharm 2022.3 brings an improved experience for working with asynchronous code in the Python Console – to call coroutines you can now use the await keyword outside of functions. The Python Packages tool window can now help you find, install, and delete packages from the Anaconda package library (in addition to PyPI). For pandas DataFrames there are new options to hide columns, use pagination to quickly go through rows, and export DataFrames in various formats.

Download PyCharm 2022.3

Conda in the Python Packages tool window

You can now search, install, and delete Conda packages through the Python Packages tool window, just one click away from the editor.

New Settings Sync solution

The new Settings Sync plugin is capable of syncing most of the shareable settings from the platform, bundled plugins, and some third-party plugins.

Enhanced UX for pandas DataFrames [Professional Edition]

Customize the way you work with DataFrames! You can now hide columns, use pagination to look through rows, export DataFrames in various formats, and more!

Improvements to docstrings rendering in Quick Documentation

The Quick Documentation popup now helps you quickly see the class attributes, as it now displays the Attributes section of the class docstrings. This also works for inherited class attributes and attributes of data classes.

asyncio support for the Python Console

The built-in Python Console now supports using the await keyword outside the function to quickly run a coroutine. PyCharm 2022.3 also adds asyncio support for the debugger. While useful for debugging asynchronous code, this feature is experimental and might not be fully stable. To enable it, follow the steps described here.

Frontend Development [Professional Edition] Vitest support

PyCharm now supports Vitest, a Vite-native unit test framework! You can run, re-run, and debug your tests in all of the key ways you’d expect, including through gutter icons. Also, watch mode is enabled with the All Tests scenario by default. Snapshot testing and coverage are supported in watch mode, too, giving you near-instant feedback on coverage when coding.

New project templates for Next.js and Vite

The New Project wizard available on PyCharm’s Welcome screen now includes project templates for Vite and Next.js. We’ve also updated the project template for Vue to make sure it follows the latest standards.

Vue updates

PyCharm can now take care of unresolved imports and will offer suggestions for importing Vue components. We’ve also supported the props destructure syntax, improved the behavior of code completion and type checking for Vue library component props, and fixed several Nuxt 3 issues.

Redis support [Professional Edition]

With new Redis support, you can connect to Redis Single Instances, explore key values in the data viewer, write and execute Redis queries with the help of our smart coding assistance, and more.

Learn more

These are the most notable changes brought by the PyCharm 2022.3 release. You can find a more detailed list of updates on the What’s New page and in the release notes.

We’re always keen to receive your feedback about the new features and updates! Please share your thoughts and suggestions on Twitter, via our issue tracker, or in the comments section below.

Categories: FLOSS Project Planets

FSF Events: Free Software Directory meeting on IRC: Friday, December 30, starting at 12:00 EST (17:00 UTC)

GNU Planet! - Thu, 2022-12-01 12:39
Join the FSF and friends on Friday, December 30, from 12:00 to 15:00 EST (17:00 to 20:00 UTC) to help improve the Free Software Directory.
Categories: FLOSS Project Planets