Copying strings in a less ugly way

So, for one reason or another, I was in a method that I had limited power to refactor. In this method there where two strings that, if certain conditions applied, needed to have the same value (Yeah, don’t get me started).

Let’s say it looked something like this:

s1 = "Hello";
s2 = "World";
s3 = "Hello Brave New World";

[...]
if(SomeCondition)
{
s1 = s3;
s2 = s3;
}
[...]

“So, what the hell can I do to make it less of an eyesore?”, I thought to myself.

After thinking about it, I came up with this:

private static (string, string) DuplicateString(string s)
{
    return (s, s);
}

And the end result looks like this instead:

s1 = "Hello";
s2 = "World";
s3 = "Hello Brave New World";

[...]
if(SomeCondition)
{
(s1, s2) = DuplicateString(s3);
}
[...]

Still not good, but a whole lot better!

Working with XML in C#

So, this is a post I’ve been meaning to write pretty much since I started the blog. In my adventures in programming I come across XML formats every now and then and I always forget how to create C# classes out of them. Instead of trying to find it on Google the next time, I’ll just read this post instead.

So, let’s say we have an XML file looking like this:

<?xml version="1.0" encoding="utf-8"?>
<Record>
<RecordArtist>Oasis</RecordArtist>
<RecordName>Be Here Now</RecordName>
<RecordYear>1997</RecordYear>
<RecordLabel>Creation Records</RecordLabel>
</Record>

Pretty simple structure, right? Let’s go through the motions of creating a class for this. First, you’ll need to convert the XML to XSD. Find the “Developer Command Prompt for VS 2019” and browse to where the above XML is located.

xsd.exe Record.xml

That’s it! Now you’ve got a XSD that can be used to create a C# class.
To create the class, go:

xsd.exe Record.xsd /c /n:RecordNamespace

Done! Use it in your project and you’re all set.

EDIT: A word of caution: You should always prefer having an XSD to start with instead of an XML. If the XML file doesn’t have all the fields they, naturally, won’t show up in the created class. But you already knew that.

Python and football

So, I haven’t been playing too much with Python recently but the opportunity came up today so I jumped right in!

To provide some context, we are using Octopus to deploy our stuff to Azure, and in Octopus I wrote a Powershell hack so that Octopus reported to Slack the name of the person doing the deploy. In our build chain we are using TeamCity to push our stuff to Octopus. In true CI/CD spirit some of our things are automatically deployed to Azure when a TC build is finished. When that happens, the person doing the deploy is some generic service account user in Octopus.

My colleague thought this was a boring thing: “When this happens, what about printing the name of a random footballer?” Said and done, one Google search later I found, and signed up for, Football-Data.org. They offered a couple of leagues free of charge with a rate limit of 10 requests per minute. Great! Let’s do some Python:

import random as r
import requests as req
import json as j

def Maximum(obj):
    return len(obj) - 1

def prettyPrintJson(obj):
    text = j.dumps(obj, indent = 4)
    print(text)

Minimum = 0
TokenKey = "X-Auth-Token"
TokenValue = ""
compUrlPrefix = "https://api.football-data.org/v2/competitions/"
compUrlSuffix = "/teams"
teamUrl = "https://api.football-data.org/v2/teams/"
competitions = [2000, 2001, 2002, 2003, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2021]
T = "teams"
I = "id"
S = "squad"
N = "name"

compResponseJson = req.get(compUrlPrefix + str(competitions[r.randint(Minimum, Maximum(competitions))]) + compUrlSuffix, headers={TokenKey: TokenValue}).json()
#prettyPrintJson(compResponseJson)

randomTeam = compResponseJson[T][r.randint(Minimum, Maximum(compResponseJson[T]))][I]

teamReponseJson = req.get(teamUrl + str(randomTeam), headers={TokenKey: TokenValue}).json()
#prettyPrintJson(teamReponseJson)

randomName = teamReponseJson[S][r.randint(Minimum, Maximum(teamReponseJson[S]))][N] + " (" + teamReponseJson[N] + ")"

print(randomName)

So basically, I pick a random league from the free ones, pull all the teams, pull a random team and then pull a random player for that team.

The output looks like this:

Matteo Raspa (UC Sampdoria)

I figured it was a nice touch to add the name of the team as well since I sometimes had no idea what player it was. Suddenly, Emre Can showed up in the result. Seems like the coaches are in there as well!