Thursday, March 2, 2023

Python: importing settings or dependency injection

Python: importing settings or dependency injection

Two common approaches to configuring a python application are importing settings and dependency injection.

Importing settings involves creating a settings module or file that contains all the values and configurations needed for your application to run. These settings can then be imported into other modules and used directly. The advantage of this approach is that it is simple and easy to understand, and can work for small projects. The disadvantage is that it increases coupling to the settings module and to the way that those settings are collected. It also makes it harder to unit test a module or class without having to resort to mocking the settings module in some way.

Dependency injection involves passing dependencies (such as settings) into a class as parameters, typically using a class method that sets some class variables.

The advantage of this is that the class needs no knowledge of the settings module or how those settings are loaded. It also makes testing easier because tests can either rely on the classes default settings or inject their own test settings. The class can also be re-used more easily.

Compare these two examples:

Importing Settings

settings.py

# Some settings as class variables.. maybe use pydantic BaseSettings
class Settings():
    some_setting = "some_value"

some_class.py

from settings import Settings

class SomeClass():
    SOME_SETTING = Settings.some_setting

    def __init__(self):
        print(f"SOME_SETTING = {self.SOME_SETTING}")

Dependency Injection

settings.py

# Some settings as class variables.. maybe use pydantic BaseSettings
class Settings():
    some_setting = "some_value"

    # Inject the settings into the SomeClass class
    from some_class import SomeClass
    SomeClass.configure(some_setting=Settings.some_setting

some_class.py

class SomeClass():
    SOME_SETTING = "default"

    @classmethod
    def configure(some_setting):
        SOME_SETTING = some_setting

    def __init__(self):
        print(f"SOME_SETTING = {self.SOME_SETTING}")

Thursday, October 31, 2019

Anytone AT-5555N Programming port


Fitting a female to female 3.5mm panel mount coupler to the AT-5555N so that I can expose the programming interface using the stock programming cable. 
  The 3.5mm socket compatible with FTDI serial cables.
 


Friday, October 12, 2018

Getting FlashFloppy onto the Gotek SFRC922D

These flash floppy emulators from Gotek are great. But they are even better when they have the FlashFloppy firmware loaded.

The SFRC922D model I have seems to have some extra holes under the serial/boot jumpers.

I chose to use the USB/DFU programming method for which you will need a USB A-A cable.

Here's how I put the Gotek into boot mode for DFU:


Jumpers for boot/DFU mode. Wirelinks above the white connector.


Once in boot mode you can program the device using the dfu-util:

I'm using OSX, and used homebrew to install the dfu-util.

brew install dfu-util

dfu-util --list

dfu-util 0.9

Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2016 Tormod Volden and Stefan Schmidt
This program is Free Software and has ABSOLUTELY NO WARRANTY
Please report bugs to http://sourceforge.net/p/dfu-util/tickets/

Deducing device DFU version from functional descriptor length
Found Runtime: [05ac:8206] ver=1965, devnum=2, cfg=1, intf=2, path="93-2", alt=0, name="UNKNOWN", serial="UNKNOWN"
Found DFU: [0483:df11] ver=2200, devnum=1, cfg=1, intf=0, path="29-2", alt=1, name="@Option Bytes  /0x1FFFF800/01*016 e", serial="STM32"
Found DFU: [0483:df11] ver=2200, devnum=1, cfg=1, intf=0, path="29-2", alt=0, name="@Internal Flash  /0x08000000/128*002Kg", serial="STM32"

dfu-util -a 0 -s :unprotect:force -D FF_Gotek-v0.9.30a.dfu

dfu-util 0.9

Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2016 Tormod Volden and Stefan Schmidt
This program is Free Software and has ABSOLUTELY NO WARRANTY
Please report bugs to http://sourceforge.net/p/dfu-util/tickets/

Match vendor ID from file: 0483
Match product ID from file: df11
Deducing device DFU version from functional descriptor length
Opening DFU capable USB device...
ID 0483:df11
Run-time device DFU version 011a
Claiming USB DFU Interface...
Setting Alternate Setting #0 ...
Determining device status: state = dfuERROR, status = 10
dfuERROR, clearing status
Determining device status: state = dfuIDLE, status = 0
dfuIDLE, continuing
DFU mode device DFU version 011a
Device returned transfer size 2048
DfuSe interface name: "Internal Flash  "

Device disconnects, erases flash and resets now


dfu-util -a 0 -D FF_Gotek-v0.9.30a.dfu

dfu-util 0.9

Copyright 2005-2009 Weston Schmidt, Harald Welte and OpenMoko Inc.
Copyright 2010-2016 Tormod Volden and Stefan Schmidt
This program is Free Software and has ABSOLUTELY NO WARRANTY
Please report bugs to http://sourceforge.net/p/dfu-util/tickets/

Match vendor ID from file: 0483
Match product ID from file: df11
Deducing device DFU version from functional descriptor length
Opening DFU capable USB device...
ID 0483:df11
Run-time device DFU version 011a
Claiming USB DFU Interface...
Setting Alternate Setting #0 ...
Determining device status: state = dfuERROR, status = 10
dfuERROR, clearing status
Determining device status: state = dfuIDLE, status = 0
dfuIDLE, continuing
DFU mode device DFU version 011a
Device returned transfer size 2048
DfuSe interface name: "Internal Flash  "
file contains 1 DFU images
parsing DFU image 1
image for alternate setting 0, (2 elements, total size = 89556)
parsing element 1, address = 0x08000000, size = 26092
Download [=========================] 100%        26092 bytes
Download done.
parsing element 2, address = 0x08008000, size = 63448
Download [=========================] 100%        63448 bytes
Download done.

done parsing DfuSe file

Friday, September 21, 2018

Acorn A5000

Acorn A5000

Very dirty. And Rusty.






After cleaning with acetic acid & IPA


Disk drive flashes with a fault code: 00010059  Simulate &00010059
  • CMOS unreadable
  • PC-style IO world detected
  • ARM 3 fitted/ARM ID read and not ARM2
  • Long memory test performed
  • Self-test due to power-on

  • So some work needed around the CMOS.
UPDATE: 2018-09-23

Fixed the corroded CMOS tracks

It Lives!

Sorted the rust & painted (Needs lettering)

Almost the right grey, but shows up the disk drive :-o


Tuesday, September 18, 2018

RiscPC 700

Poor old RiscPC suffered the common battery leak from the PCB mounted NiCd.

Sadly it caused some damage to tracks around the CMOS clock and memory.

The charging circuit for the NiCd was also damaged.

Things I know that work now:

* Charging - Replaced the failed 180R resistor with a through-hole type as I needed to also get power from the diode and the track had disintegrated. Not pretty.
* CMOS I2C - I watched the waveforms on the logic analyser.
* Sound & Video from VIDC


Battery replacement. NiMh hidden well away from the PCB


 Life! for the first time since 2009!


Little bit better!
Some more cleaning of SIMs & VRAM
Passable, but I think the VRAM needs replacing now.

Well, that's the most life I've had out of this recently, so I'm a bit happier with it. Still some way to go.

Next steps.  Some new RAM and VRAM.



Tuesday, July 24, 2018

Hacking the Sureflap Dual Scan Cat Flap

Hacking the Sureflap Dual Scan Cat Flap


To get the RFID codes for the 134.2 KHz Cat chip ID's



 Aha! A PIC. And an ICD port next to it. PICKit3 at the ready.
I'd quite like to get the firmware off this if possible.

Atari ST

Atari ST


A good friend found these in the loft and thought I might find some use for them.
 There was some interesting software came with them. Transputers! Occam!
 And levels of brownness not seen since the 1970's.
 This one cleaned up ok though.

Acorn A300

Acorn A300


Bit of a Frankensteins Monster. Bits from A300 / A410 and many upgrades.

Unfortunately this one suffered a little from poor storage at some point.

Luckily most of the damage is just the back panel. Everything else just needs a bit of a clean.

 There's some great bits of wire linking and factory mods on this board.

 And a few of my own mods for memory and ARM 3
 Bit of rust.. Clean up and repaint..

Sunday, July 15, 2018

Tatung Einstein


Had a great time at the Newbury Radio Rally and an amazing bit of luck finding this Tatung Einstein from 1984 in pretty good condition. 
In another bit of amazing luck one of my friends had a bundle of Manuals, magazines and some disks.

Sunday, November 2, 2014

Project Transputer


Project Transputer is coming along nicely.



I repurposed the board from another project. It's got Bluetooth and Wifi modules already mounted and 5V and 3V regulators.




Thursday, February 13, 2014

From little Acorns..

Just reading some of this: http://go.theregister.com/feed/www.theregister.co.uk/2014/02/13/reg_curriculum_rfc/

I don't really want to get too involved with the arguments about it, but for what it's worth...

I've been programming for a living since leaving school in 1987. My first job was programming which was rather unusual at the time. In fact I started programming for my uncles company before leaving school and had been programming for a few years before that.  I didn't learn programming from school, I learned it at home programming my ZX81, which was about all you could do with it, but that was a good thing I think.

I got interested in computing from several sources, I was already interested in electronics and some of my friends were too and some had computers.  My school had a lunchtime computer club where pupils got involved with setting up the BBC micros and RML 380Z's and had small programming projects they worked on.  All this created an interest and a desire to get my own computer. I remember it took a bit of talking my parents round to splashing the £100 on it, and my uncle helped with that argument.

I spent a couple of years programming my ZX81 and later Spectrum and BBC (I did have  games on tape & disk as well - not all programming). I think it was the fact you had to almost construct the basic machine, connect tape recorders or disk drives and had just a command prompt that forced a certain level of ingenuity to get any reward from the machine.  But you did get a reward when programs worked or you found a new way of getting the machine to do something. Myself and my friends also used to have fun programming the demo computers in Boots and WHSmiths.

Which brings me on to the Raspberry Pi.  There has been a desire to try and recreate that era of discovery and ingenuity that the BBC Micro and early home microcomputers generated.  I think that's where the RPi is coming from and it's a nice cheap introduction to computing.  But it still takes a certain type of person to get properly interested in it.

  I saw an article the other day (can't find the source now) which was about making the Raspberry Pi even easier to use.  I balked a bit at that.  I personally think that's the wrong way to go. If you want something easy to use then there's plenty of cheap Android tablets for that.  I think what it needs is to be more basic perhaps running some more basic OS that drops you into a command prompt much like the Beeb did with languages on hand.  Possibly even something like the RiscOS OS from the Archimedes.  (These OS's are practically bullet proof).  But they provide the basic raw materials for learning about computing.   Once those are mastered the desire for more complex OS's and programs may well grow and the RPi would be able to grow with that. 

I have to assume that schools are running computer clubs but I'm not sure what form they take these days. Perhaps that's an area that might be used to get kids interested in programming again if it isn't already. Probably better than forcing everyone to do it. From my experiences with my own children and with quite a number I met when I took my BBC Micros to Maker Faire, kids love using the old machines, some were appalled that they didn't have Facebook, but most liked the basic games and some even had a stab at programming.

I think all is not lost (yet).

//

//images from http://commons.wikimedia.org

Thursday, February 6, 2014

Amazon AWS basics in Rails

Listing Amazon EC2 instances

# Class for VM's 
class Vm
  include ActiveModel::Validations

  validates_presence_of :name, :status

  attr_accessor :id, :name, :dns, :status
  @@ec2 = AWS::EC2.new()

  def self.all
    return @@ec2.instances.inject([]) { |m, i|
      tags = i.tags.to_h
      name = tags['Name']
      m.push Vm.new( i.id, name, i.dns_name, i.status )
    }
  end

  def initialize( id, name, dns,  status )
    @id     = id
    @name   = name
    @dns    = dns
    @status = status
  end

end

Listing Amazon S3 buckets

# Class for S3 Storage
class Storage
include ActiveModel::Validations
 
validates_presence_of :name
 
attr_accessor :name
@@ec2 = AWS::S3.new()
 
def self.all
return @@ec2.buckets.inject([]) { |m, i|
    m.push Storage.new( i.name )
}
end
 
def initialize( name )
@name = name
end
 
end

AWS Config

config/initializers/aws-sdk.rb
AWS.config({
  :secret_access_key => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
  :access_key_id => 'YYYYYYYYYYYYYYY',
  :region => 'us-west-2',
})



Thursday, January 31, 2013

Handling and Tyre wear patterns

Racing last night at Bashley the car was still quite unstable. For the last few races I have been changing one setting at a time to pin down which are making improvements. So far I've stiffened the front springs, moved the shock positions on the rear and toed out the steering a little. The car was better but still quite hard to tame the back end..
If you look closely at the tyres you can just about make out that the insides of the tyres are a little duller meaning that the majority of the cars weight has been on the insides even during dynamic handling. This is a big clue to start looking at the camber adjustments.
Camber is the angle of the wheel relative to the track looking from the front of the car. 0 degrees means the wheel is dead vertical. -5 degrees means the top of the wheel is angled toward the centreline of the car. A little negative camber can be useful, but evidently I had too much. I rather unscientifically reduced the amount and in the next heat the car was far more controllable with more power going to the track.

Wednesday, June 22, 2011

NADARS Newbury Radio Rally

Each year on Fathers day the Newbury and District Amateur Radio Society organise their rally which has been at the Newbury Showground for the last few years.


For anyone not familiar, radio rallies or hamfests as they are sometimes known are a show organised by an amateur radio society. They usually take the form of a field with trade stands, a club stand and a car boot section. There is often a very varied mix of things on sale from old wartime radios, military surplus, old broadcast equipment, telecom equipment, components to computers and all manner of bits and pieces.




 This has always been one of the nicest rallies and one which has been on my list of favourites since about 1993, so I thought I'd share a few photos of the day.

We got there a little late, but it was still a good day. I picked up a few bits for the DATV station I'm planning on building, an NDS CSR820 MPEG2 IRD and some L-Band filters and splitters. I also met an old friend which was nice.
It was a bit of a drive (4 hours) but still happy I went.

73 de M1BSC :-) 

NADARS - http://www.nadars.org.uk/ 

Wednesday, March 16, 2011

Maker Faire Newcastle 2011

 Maker Faire Newcastle, UK 2011




Last week Makers and Hackers from all over the world converged on the Centre for Life in Newcastle-Upon-Tyne for the UK's third Maker Faire.  Maker Faire is an event organised by O'Reilly's Make magazine to bring together art, crafts, technology, engineering and science projects and to celebrate and encourage the Do it Yourself mindset.




The BBC's Research and Development and Television Platforms groups were there meeting the public and making friends with other Makers. We had several exhibits ranging from the fun Blink-o-tron with its trapped Weeping Angel, Conduct your own orchestra to the more serious Universal Control which allows subtitles to be sent to mobile devices.




 The Maestro demonstrates in an entertaining way our ability to interface to the Microsoft Kinect and to use thedata provided to control video playback. Basically you use the baton and move your hands in the manner of an orchestral conductor and you can control the volume and tempo of the music.















We also showed a concept device which presented a different way of accessing the BBC's Radio archive. The 'Wayback Machine' is designed to look as much like a traditional radio as possible, retaining simple controls, but allows the date and time to be 'tuned' in.













An example of the BBC's collaboration with Newcastle University was also on show with the Tune Table, a device which was developed by one of the universities students while on placement with R&D.


The Tune Table was built to explore the control metaphore and investigate its applications for areas such as video editing.




We also tried to entertain (and sometimes scare) some of the visiting children with our Blink-o-tron and its trapped Weeping Angel from Doctor Who.  This is an example of some basic microcontroller hacking using an Arduino, some LED's and some pieces of plexiglass which works on he principle of Pepper's Ghost. Don't Blink!









There was also a homebrew tapeless camera system on display which lead us nicely into conversations about how the BBC are moving towards using systems such as INGEX and the Digital Media Initiative (DMI).





We also met with and talked to a great many members of the public about the work we do and we dealt with many enquiries about our employment opportunities at BBC North in Salford.




What the other bloggers are saying:-


http://www.youtube.com/watch?v=Bked4XQz89g
http://vickyteinaki.com/blog/maker-faire-uk
http://zine.openrightsgroup.org/reviews/2011/maker-faire-2011
http://zuzebox.wordpress.com/2011/03/15/maker-faire-uk-2011/
http://www.youtube.com/watch?v=yhNohhCnX2I&feature=related


Credits:
  Video: Brendan Crowther, Laura Chalmers, Ian Calvert, Bruce James
  Photos: Bruce James
  Blog: Bruce James, Yameen Rasul
  Organisation: Max Leonard, Yameen Rasul
  On the stand: Max Leonard, James Barrett, Tom Bartindale, Bruce James Andrew Bowden, Ian Calvert, Mo McRoberts