Python vs C#

I have read the article https://www.red-gate.com/simple-talk/development/dotnet-development/10-reasons-python-better-than-c-sharp

I think that deserves an answer –  so I take all points to answer to them. You can replicate the code in C# 10 / .NET 6 with a globalusings.cs with the following content:


global using static System.Console;
global using static System.Linq.Enumerable;
global using static System.IO.Directory;
global using static System.String;
global using static System.Math;

So let’s start:

Point 1 - Indented code blocks

//Point 1 - Indented code blocks
//With curly braces I can indent whatever way I want - I am not constrained

using System.Collections.Generic;

foreach (var i in Range(1, 11))
{
if (i % 3 == 0)
WriteLine("fizz");
else if (i % 5 == 0)
WriteLine("buzz");

}

Also , when I want to comment something , with C# I know when the else if is
terminating without counting tabs. Just see where the { is closing with }

Point 2 -  Self-declaring Variables

Seriously? What is the difference between Python
//meaning_of_life = 42
and C# ? Just a var ?

//Point 2 - Self-declaring Variables
var meaning_of_life = 42;
Point 3 -– Those modules

It is the same code as Python

foreach (var file in EnumerateFiles(@"C:\")){
 WriteLine(file);
}
Point 4 – Simplicity of Data Structures

Quoting : Purists will say that you need all the different data types that C# provides, but trust me, you don’t.
I want to think about how to modelate a CallCenter waiting for phones – Stack or Queue ?
I want to think about how to  modelate a CallCenter waiting for phones with business consumer that has priority ?https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.priorityqueue

Point 5 - Slicing

Same code in C#

var sins = new[]{ "pride", "envy", "gluttony", "greed", "lust", "sloth", "wrath" };
//var selected_sins = sins[1..(sins.Length-1)];
var selected_sins = sins[1..^1];
WriteLine(Join(" ", selected_sins));
Point 6 – For loops

Same syntax in C#

foreach (var i in Range(0,5))
{
WriteLine(i);
}
// words in a string
string[] words = { "Simple", "Talk" };
foreach (string word in words)
{
WriteLine(word);
}

Quoting : “(unlike in C#, where sometimes you use for and sometimes foreach)”
Please enumerate an array backwards in python without reversing

7 – List comprehensions

This is similar code in C#

var nr = Range(1, 11).Where(it => it % 2 == 0).Select(it => Pow(it, 3));
WriteLine(Join(" ", nr));
8 – Sets

Same example

//this contains some duplicates
var languages = new[] { "C#", "Python", "VB", "Java", "C#", "Java", "C#" };
//more dense than Python
languages = new HashSet<string>(languages).ToArray();
//this will give: ['C#', 'Java', 'Python', 'VB']
WriteLine(Join(" ", languages));

Second example

//create two sets: Friends characters and large Antarctic ice shelves
var friends = new[] { "Rachel", "Phoebe", "Chandler", "Joey", "Monica", "Ross" };
var ice_shelves = new[] { "Ronnie-Filchner", "Ross", "McMurdo" };
// show the intersection (elements in both lists)
WriteLine(Join(" ", friends.Intersect(ice_shelves)));
//show the union (elements in either list)
WriteLine(Join(" ", friends.Union(ice_shelves)));
// show the friends who aren't ice shelves
WriteLine(Join(" ", friends.Except(ice_shelves)));
// elements in either set but not both -- more difficult
WriteLine(Join(" ", friends.Union(ice_shelves).Except(friends.Intersect(ice_shelves))));
9 – Working with files and folders
//# list of 3 people
var people = new[] { "Shadrach", "Meshach", "Abednego" };
//# write them to a file
File.WriteAllLines(@"C:\all\a.txt", people);
10 – The quality of online help

See https://docs.microsoft.com/ – good tutorials

Friday Links 452

  • Idempotent DDL Scripts That Always Achieve The Same Result – Making Changes Only Once: Stairway to Exploring Database Metadata Level 6 SQLServerCentral
  • Overengineering in software development
  • Doom s classic re-releases now run at 60fps and support free add-ons – The Verge
  • .Net Core DI, How to register all existing assignable types in an assembly – CodeProject
  • uuidjs/uuid: Generate RFC-compliant UUIDs in JavaScript
  • 6 Ways to Unsubscribe from Observables in Angular – Bits and Pieces
  • 10 Chrome Console Utility APIs You Probably Never Used
  • Extremely useful lodash methods – Level Up Coding
  • A Decade of Cross Functional Requirements (CFRs) – Sarah Taraporewalla
  • Sagas
  • Database per service
  • How to Get the Most of Your Coverage Report – Typemock
  • Welcome | Applied Cloud Stories
  • cloud-annotations/cloud-annotations-client: Source for the Cloud Annotations tool
  • microsoft/VoTT: Visual Object Tagging Tool: An electron app for building end to end Object Detection Models from Images and Videos.
  • David K. <ع� on Twitter: “Developers sitting in meetings and fixing CSS bugs all day: “Funny how I get paid for this” Developers working on open-source software that thousands of companies and developers depend on: “Funny how I don’t get paid for this””
  • Copilot for VS Code – Visual Studio Marketplace
  • 3 Ways to Debounce HTTP Requests in Angular – Bits and Pieces
  • 11 Recommended Tools for Frontend Developers – Bits and Pieces
  • microsoft/ClearScript: A library for adding scripting to .NET applications. Supports JavaScript (via V8 and JScript) and VBScript.
  • Automatic release GitHub with version

    For having releases published automatically on Github , I use this tagged release.yml file as an workflow

    name: “tagged-release”
    on:
    push:
    tags:
    – “v*”
    jobs:
    build:

    runs-on: ubuntu-latest

    steps:
    – name: ‘Checkout Github Action’
    uses: actions/checkout@master

    # code to build the source
    – name: Archive production artifacts
    uses: actions/upload-artifact@v2
    with:
    name: nameOfTheApp
    path: ‘src/…/*’
    retention-days: 1

    – name: Archive Release
    uses: thedoctor0/zip-release@master
    with:
    type: ‘zip’
    filename: ‘nameOfTheApp.zip’
    path: ‘${{github.workspace}}/src/…’
    exclusions: ‘*.git* /*node_modules/* .editorconfig’

    – name: Release
    uses: softprops/action-gh-release@v1
    with:
    files: |
    nameOfTheApp.zip

    – name: delete older-releases@v0
    uses: dev-drprasad/delete-older-releases@v0.2.0
    with:
    #repo: / # defaults to current repo
    keep_latest: 2
    #delete_tag_pattern: beta # defaults to “”
    env:
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

    FileExtension–PostMortem- part 7

    It was interesting to wrote this project.  At the final, it features an web site (https://fileextension.azurewebsites.net/ ) and a NuGet package ( https://www.nuget.org/packages/FileExtension/ ). Also , I have learned how to start Windows Terminal to start Angular / .NET Core / Tests

    wt new-tab -p “Windows PowerShell” -d . cmd /k “cd src && dotnet build && cd RecognizeFileExtWebAPI && dotnet watch run”  ;split-pane -p “Windows PowerShell” -d . cmd /k “cd src && dotnet build && cd TestFileExtensions && dotnet watch test” ;  split-pane -p “Windows PowerShell” -d . cmd /k “cd src && cd FileExtensionAng && npm i && ng serve -o”

    and refreshed my GitHub actions knowledge ( I am very fond of CI – and , if I can, CD )

    Also, I have now a better understanding of a csproj file in relation with nuget :

    <ItemGroup>
       <None Remove=”plugins\FileExtension.targets” />
    <Content Include=”plugins\FileExtension.targets”>
       <PackagePath>build\net5.0\</PackagePath>
       <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
    </ItemGroup>

    <ItemGroup>
       <Content Include=”plugins\**\*.dll”>
         <PackagePath>build\net5.0\plugins\</PackagePath>
         <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
       </Content>
    </ItemGroup>

    It was refreshing to know that, even I like the Skinny Controllers concept, however the WebAPI is not so light .

    Hopefully someone will find the package useful – or at least the site  https://fileextension.azurewebsites.net/

    FileExtension–part 6 – CI/CD

    So now how to make visible all those coding ?

    For tests –CodeCov,  https://app.codecov.io/gh/ignatandrei/FileExtension hosts for free to see the coverage

    For documentation – ReadTheDocs,  https://fileextension.readthedocs.io/en/latest/ hosts for free.

    For packaging  – NuGEt https://www.nuget.org/packages/FileExtension/

    For more detailed analysis Sonar https://sonarcloud.io/summary/overall?id=ignatandrei_FileExtension can show code smells and bugs and more

    GitHub actions https://github.com/ignatandrei/FileExtension/actions allows me to run all those automatically and gather results

    For showing a website – Azure  – https://fileextension.azurewebsites.net/ can show the usage.

    I was thinking that it is enough for the people to understand the application

    [ADCES]Devops By Docker & Life after Java 8

    Details

    Presentation 1 : Practical DevOps: One-Click Deployment using Docker
    Presenter: Andrei Micuda,https://github.com/andrei-micuda/
    Description:
    Every developer wants to deploy and maintain the applications he develops with ease. Although this seems like a daunting task at first, you can achieve a one-click deployment pipeline by only using a few Docker containers. Join us if you want to find how to use Docker to automate your development workflow and build a fully customizable and easy-to-understand CI/CD pipeline from scratch.
    Presentation 2: Life after Java 8
    Presenter : Cosmin Popescu, https://www.chestiiautomate.ro/

    FileExtension–Tests–part 5

    The tests are the easy part. I have put some files on a folder( https://github.com/ignatandrei/FileExtension/tree/master/src/TestFileExtensions/TestFiles ) and test if the extension matches the recognizer.

    As NuGet packages I used

    FluentAssertions

    xunit

    LightBDD.XUnit2

    FluentAssertion allows me to write more readable tests , like 

    AllExtensions.Should().HaveCountGreaterThanOrEqualTo(number);

    LightBDD allows me to write tests in a more “business “ way :

    await Runner
         .AddSteps(Given_The_Recognizer)
         .AddAsyncSteps(_ => When_Read_The_File(fileName))
         .AddSteps(
             _ => Then_Should_Recognize_File(fileName),
             _ => Then_The_Extension_Matches(fileName)
         )
         .RunAsync();

    You can see the result at https://fileextension.readthedocs.io/en/latest/BDD/LightBDDReport/ = go down to see the Given / When /

    Also, for enumerating files from the folder to test, I use ClassData

    [ClassData(typeof(DirectoryTestData))]
    public async void TestMultipleFiles(string nameFile)

    Andrei Ignat weekly software news(mostly .NET)

    * indicates required

    Please select all the ways you would like to hear from me:

    You can unsubscribe at any time by clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

    We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp's privacy practices here.